Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: logic behind the print function

by masak (Scribe)
on Dec 29, 2009 at 16:18 UTC ( #814777=note: print w/ replies, xml ) Need Help??


in reply to logic behind the print function

The original poster is, it seems, confused by two things: first, that the result of a (successful) print is 1, and not the thing printed. Second, that the parentheses around (5+2) delimit the arguments sent to the inner 'print' call. Both of these are obvious to more seasoned Perl programmers, but not to newbies.

It is my irreverent but humble duty to report that Perl 6 'fixes' the second of these points of confusion, by having print(5+2) and print (5+2) (note the space) mean different things. In the first instance, (5+2) is parsed as an argument list with one argument, 5+2. In the second instance, because of the space after print, the parser expects a non-parenthesized list of arguments, and finds (5+2)+10.

So, a Perl 6 inplementation prints "171": "17" for the correct result of the calculation (5+2)+10, and "1" for the outer print of the return value of the inner print.


Comment on Re: logic behind the print function
Select or Download Code
Re^2: logic behind the print function
by masak (Scribe) on Dec 29, 2009 at 16:24 UTC
    Oh, and users of both Perl 5.10 and Perl 6 would likely have instinctively used say rather than print to begin with; say is shorter to write, and gives you an extra newline for free, making the output easier to parse for human eyes.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-10-24 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (138 votes), past polls