Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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]
[choroba]: option one is install everything from the vendor
[choroba]: option two is install everything by hand
[choroba]: option three is mixed the two, but know well what you're doing
[vedagiri89]: right..let me check

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2018-06-19 11:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.