Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://814777]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-01-17 21:53 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (158 votes). Check out past polls.