Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Operator precedence

by Kickstart (Pilgrim)
on Apr 09, 2001 at 22:33 UTC ( [id://71100]=perlquestion: print w/replies, xml ) Need Help??

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

Could someone give a "this is how you avoid problems" reference/post about operator precedence? I've come across something that I was curious about with it. Examples:

$foo = 'This is a CAPITAL test, man'; $bar = 'IS A cap'; if ((lc $foo) =~ (lc $bar)) { print lc $foo; print "\n"; print lc $bar; print "\n"; print 'YES'; } else { print lc $foo; print "\n"; print lc $bar; print "\n"; print 'NO'; }

...the above works fine. But a small change as this:

if (lc $foo =~ lc $bar)

...breaks it.

Thanks!

Kickstart

Replies are listed 'Best First'.
Re: Operator precedence
by MeowChow (Vicar) on Apr 09, 2001 at 22:45 UTC
    Looking at perlop, you will see that lc, a named unary operator, is lower in precedence than the binding (pattern match) operator, "=~". So your second example is actually evaluated as:
    if (lc ($foo =~ (lc $bar))
    Operator precedence can be a tricky thing, and it usually bites you in the posterior just when you think you've got it down.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Operator precedence
by Dominus (Parson) on Apr 09, 2001 at 23:01 UTC
      That's exactly what I was looking for...nice and clear writing about something which is most definitely not a clear subject.

      Trying to figure this all out is 'Perl masturbation': You keep pulling and pulling and eventually it goes all over the place, but you never actually get to build anything with it.

      :)

      Kickstart

Re: Operator precedence
by knobunc (Pilgrim) on Apr 09, 2001 at 22:56 UTC

    I am assuming that you are looking for ways people remember precedence and I am not sure that there are any. For the most part they follow mathematic syntax or C. If there is extra stuff then for the most part it was where Larry Wall et al. thought made sense.

    Well you could do a perldoc perlop and read the precedence rules which state that =~ has a higher precedence than lc (a named unary operator). After much memorizing you could be an all poweful pecedence ninja and never need extra parentheses.

    However, if you want other people to be able to read it easily then I would recommend putting in parentheses where it makes sense to do so. Also using the 'english' 'and', 'not', and 'or' operators instead of &&, !, or || since they have very low precedence and tend not to need parentheses around the clauses nearest them. (Not that you had any of those guys so may already follow that rule)

    So, because I like functional notation, I would write:

    if ( lc($foo) =~ lc($bar) ) { ... }

    Although to be absolutely honest I would have written:

    if ( index( lc($foo), lc($bar) ) != -1 ) { print "Yes\n"; } else { print "No\n"; }

    Because it is probably faster than using the regexp enging.

    -ben

    Bah, sorry for the duplicate post, there were no responses when I started.

Re: Operator precedence
by mbond (Beadle) on Apr 09, 2001 at 22:52 UTC
    Thi may be a trvial responce, but: Why not use paran's in a more standard way, at least this is a case that seems more standard to me.... I always put function param's into ()'s ... its easier to read later, imho.
    $foo = 'This is a CAPITAL test, man'; $bar = 'IS A cap'; if (lc($foo) =~ lc($bar)) { print lc($foo), "\n", lc($bar), "\nYES\n"; } else { print lc($foo), "\n", lc($bar), "\nNO\n"; }
    Mbond.
Re: Operator precedence
by Tuna (Friar) on Apr 09, 2001 at 22:38 UTC
    perlman:perlop would be a good start

    UPDATE: A suggestion: use  use strict and  -w. Also, your change doesn't seem to "break" when I run it.
Re: Operator precedence
by Albannach (Monsignor) on Apr 09, 2001 at 23:06 UTC
    For the more general question of operator precedence, you might want to check out the -p option of Deparse, which will add parentheses as needed to make the precedence clear.

    Unfortunately it appears that my Deparse chokes on =~, but the principle is the same for something like +, and for

    if(lc $foo + lc $bar) {print $foo} produces

    if (lc(($foo + lc($bar)))) { print($foo); } -e syntax OK

    --
    I'd like to be able to assign to an luser

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://71100]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found