Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Why is $a.$b not = "$a$b"?

by tel2 (Pilgrim)
on Jul 14, 2020 at 23:10 UTC ( #11119325=perlquestion: print w/replies, xml ) Need Help??

tel2 has asked for the wisdom of the Perl Monks concerning the following question:

Beloved Monks,

I seek your wisdom re why the 4th one-liner below returns "Matches!".  The other 7 one-liners behave as I would have expected.  I thought $a.$b would be the same as "$a$b" in contexts like this - it seems to be with last 'eq' tests.  Was I mistaken?  If I print them the output looks the same.

$ perl -e '$a="A";$b="B";print "Matches!\n" if "$a$b" =~ /A/' Matches! $ perl -e '$a="A";$b="B";print "Matches!\n" if "$a$b" =~ /C/' $ perl -e '$a="A";$b="B";print "Matches!\n" if $a.$b =~ /A/' Matches! $ perl -e '$a="A";$b="B";print "Matches!\n" if $a.$b =~ /C/' Matches! # WHY??? $ perl -e '$a="A";$b="B";print "Matches!\n" if "$a$b" eq "AB"' Matches! $ perl -e '$a="A";$b="B";print "Matches!\n" if "$a$b" eq "C"' $ perl -e '$a="A";$b="B";print "Matches!\n" if $a.$b eq "AB"' Matches! $ perl -e '$a="A";$b="B";print "Matches!\n" if $a.$b eq "C"'
I added blank lines above purely for readability.
I'm running Perl 5.16.3 on Linux.
Thanks.
tel2

Replies are listed 'Best First'.
Re: Why is $a.$b not = "$a$b"?
by tybalt89 (Prior) on Jul 14, 2020 at 23:30 UTC
      Thank you tybalt89!

      Good answer!  Short, but good.

      To expand on that, Perl's operator precedence has '=~' higher than '.'.
      So to get the '.' notation to work as I want it to, I assume I should (parenthesise) it, like this:

      $ perl -e '$a="A";$b="B";print "Matches!\n" if ($a.$b) =~ /A/' Matches! $ perl -e '$a="A";$b="B";print "Matches!\n" if ($a.$b) =~ /C/'
      'eq' is lower than both of those, which explains why my 'eq' tests worked as I expected.

      Right?
Re: Why is $a.$b not = "$a$b"?
by LanX (Cardinal) on Jul 15, 2020 at 00:26 UTC
    Hint: When in doubt use B::Deparse with option -p for parenthesis

    d:\exp\version>perl -MO=Deparse,-p -E"$a=A;$b=B;print 'Matches!' if $a +.$b =~ /C/" use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', ' +state', 'switch', 'unicode_strings', 'unicode_eval'; ($a = 'A'); ($b = 'B'); (($a . ($b =~ /C/u)) and print('Matches!')); -e syntax OK

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      OK, thanks Rolf.

      tel2
      (Also a bit (or even a byte) addicted to the Perl programming language, even if it confuses the megabytes out of me at times.)

Re: Why is $a.$b not = "$a$b"?
by Marshall (Canon) on Jul 15, 2020 at 22:08 UTC
    This is a relatively small nit. But the variables $a and $b are used for special things within Perl like the sort function and sometimes other stuff. Get in the habit of using some other name like $x,$y or $xa, $xb or whatever. While there is nothing wrong with this short example, a longer example might bite back with an unexpected/or confusing result. There are lots of possible names, avoiding $a and $b for your own use is a good idea.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11119325]
Approved by LanX
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2020-10-27 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (257 votes). Check out past polls.

    Notices?