Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

According to perldoc perlop, terms have the highest precedence of everything:

A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses ...

Please consider the following code.

use 5.010; sub apple { say "apple" } sub banana { say "banana" } sub cherry { say "cherry" } apple && (banana || cherry)

Given that expressions in parentheses have topmost priority, one would expect Perl to evaluate (banana || cherry) first, which would boil down to say "banana" which would return 1, and only then to evaluate apple && (1).

So the output I more or less expected is:

banana apple

However, the output I get is:

apple banana

Please shed some light on this. Why isn't the parenthesed expression evaluated first? I understand the short circuiting nature of the logical operators — for one, this explains why cherry isn't outputted at all, but I fail to understand how && seems to have higher precedence than ( EXPR ), despite the information in the documentation.


In reply to Operator precedence by muba

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found