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

Solicitation For Ideas: Tutorial On Precedence

by Limbic~Region (Chancellor)
on Feb 15, 2005 at 14:19 UTC ( #431182=perlquestion: print w/ replies, xml ) Need Help??
Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
I have looked through the Monastery, but haven't found anything in Tutorials covering precedence. Of course one should read perlop (Perl operators and precedence), but sometimes we need something a little more basic. For instance, I often use the following code:
sub new { my $class = shift; croak "incorrect number of arguments" if @_ % 2; ... }
On the other hand, if I am using the modulus operator (%) to see if a number is divisible by another, I use:
print "$foo is divisible by $bar" if $foo % $bar == 0;
Why didn't I use if ! $foo % $bar;? The answer is precedence. In order to get that to work the way I wanted, I would need to use if not $foo % $bar; or if ! ($foo % $bar);. For people who can't commit the precedence chart to memory, they end up putting parens everywhere. While I find the following Larry quote humorous - there is too much of a good thing:

When in doubt, parenthesize. At the very least it will let some poor schmuck bounce on the % key in vi. - From perlstyle

Unfortunately, I haven't assimilated the knowledge I think is necessary to write such a tutorial. I was hoping others could provide classic pitfalls like the one I described, rules of thumb, how to remember the chart, etc. Actually, I am hoping someone just volunteers to write the tutorial and I can reap the benefits but I am willing to consolidate the responses and write the tutorial myself.

Cheers - L~R

Comment on Solicitation For Ideas: Tutorial On Precedence
Select or Download Code
Re: Solicitation For Ideas: Tutorial On Precedence
by dragonchild (Archbishop) on Feb 15, 2005 at 14:22 UTC
    Print out the chart from perlop? Seriously - I'm not seeing the benefits here. Maybe it would be better to have a tutorial on how perl parses your Perl with a section on precedence in there.

    Me, I just use -MO=Deparse and be done with it. :-)

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      dragonchild,
      We have Tutorials for many things that are already explained in the documentation from using pack/unpack to command line arguments. The point is that not all people immediately understand this information and they need it presented to them in a different way. Perhaps I am thinking more of a beginner's guide to precedence. It seems that for a lot of people who's first language is Perl - they come to expect the Do What I Mean (DWIM) approach. I often get WTF comments in #perl when I am explaining an answer by stating that "...binds more tightly than...".

      Or perhaps - I am just misjudging the need.

      Cheers - L~R

        I'm not dismissing the need for such a tutorial, but I suspect new users would be better served by a tutorial on how Perl code is chunked with a section on operator precedence. That way, you can include things like
        • Creating a list of hashrefs using map{}
        • Why doesn't print (3+5)/3; DWIM?
        • Why do some people do things like my $x = $hash_of_stuff{+shift}?

        The real topic is a larger one than the one you're looking at.

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Solicitation For Ideas: Tutorial On Precedence
by Roy Johnson (Monsignor) on Feb 15, 2005 at 14:24 UTC
    It would be nice if the tutorial were ready to release on Feb 21. That's Precedence Day (US). Or that's what Emily Latella told me.

    Caution: Contents may have been coded under pressure.
Re: Solicitation For Ideas: Tutorial On Precedence
by RazorbladeBidet (Friar) on Feb 15, 2005 at 14:25 UTC
    If the problem is solely memorization - perhaps an acrostic or a catchy little folk song of some sort.
Re: Solicitation For Ideas: Tutorial On Precedence
by phaylon (Curate) on Feb 15, 2005 at 14:28 UTC
    Hm, I think I would just write
    print "$foo is divisable by $bar" unless $foo % $bar;
    OTOH, I'm a non-native-speaker, so "if not" is often more readable for me.

    Ordinary morality is for ordinary people. -- Aleister Crowley
Re: Solicitation For Ideas: Tutorial On Precedence
by Anonymous Monk on Feb 15, 2005 at 16:02 UTC
    A tutorial? There's nothing complicated about precedence. The only difficulty lies in remembering in which of the 24 slots each of the 60+ operators lies.

    You might as well have a tutorial on the names of the days of the week, or the colours of the rainbow.

      The rainbow tutorial exists, we had the following drummed into us at school.

      Rainbow Tutorial

      Richard
      Of
      York
      Gave
      Battle
      In
      Vain

      Cheers,
      R.

      Pereant, qui ante nos nostra dixerunt!
Re: Solicitation For Ideas: Tutorial On Precedence
by tphyahoo (Vicar) on Feb 16, 2005 at 08:56 UTC
    Something that continues to blow sand in my gears is print, in conjunction with the dot operator, and lists that I mistakenly treat as scalars. I think this has something to do with precedence of the dot operator, so perhaps this could be touched on. Where *is* the dot operator documented, anyway?

    See Yet another List Versus Scalar Context Meditation

      From the perlop manual page:
                 left        terms and list operators (leftward)
                 left        ->
                 nonassoc    ++ --
                 right       **
                 right       ! ~ \ and unary + and -
                 left        =~ !~
                 left        * / % x
                 left        + - .
                 left        << >>
                 nonassoc    named unary operators
                 nonassoc    < > <= >= lt gt le ge
                 nonassoc    == != <=> eq ne cmp
                 left        &
                 left        | ^
                 left        &&
                 left        ||
                 nonassoc    ..  ...
                 right       ?:
                 right       = += -= *= etc.
                 left        , =>
                 nonassoc    list operators (rightward)
                 right       not
                 left        and
                 left        or xor
      
      (Eigth line, marked in red). And further down:
             Additive Operators
      
             Binary "+" returns the sum of two numbers.
      
             Binary "-" returns the difference of two numbers.
      
             Binary "." concatenates two strings.
      

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-10-02 08:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (52 votes), past polls