Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: eval order of args to a sub

by ikegami (Patriarch)
on May 30, 2007 at 19:56 UTC ( [id://618297]=note: print w/replies, xml ) Need Help??


in reply to eval order of args to a sub

Perl evaluates the arguments from left to right (according to the associtivity of the list seperator).

>perl -le"$i=3; print($i+0, ++$i, $i+0);" 344

However, because Perl passes all arguments by reference, the order of operation sometimes *appears* unclear.

>perl -le"$i=3; print($i, ++$i, $i);" 444

The first snippet is similar to

# $i=3; print($i+0, ++$i, $i+0); $i = 3; do { local @_; $anon0 = $i+0; alias $_[0] = $anon0; $i = $i+1; alias $_[1] = $i; $anon1 = $i+0; alias $_[2] = $anon1; &print };

The second snippet is similar to

# $i=3; print($i, ++$i, $i); $i = 3; do { local @_; $i; alias $_[0] = $i; $i = $i+1; alias $_[1] = $i; $i; alias $_[2] = $i; &print };

Can you guess what the following prints?

perl -le"$i=3; sub { $_[1]++; print @_ }->($i+0, ++$i, $i+0, $i);"

Replies are listed 'Best First'.
Re^2: eval order of args to a sub
by dewey (Pilgrim) on May 30, 2007 at 20:08 UTC
    That's very interesting. I could never have guessed that output before this thread. The creation of anonymous values (for lack of a better term) for some expressions makes me want to do some obfuscation...

    ~dewey

      All scalar variables are really only references to SVs. (There's a similar relation between arrays and AVs, hashes and HVs, etc.) Sometimes Perl creates SVs to which no variable refers, and it's possible to create more than one variable that refers to the same SV.

      +------------+ references (1:1) +------------+ | Lexical or | -----------------------> | | | Package | | SV | | Scalar Var | referenced by (1:N) | | +------------+ <----------------------- +------------+

      The "anonymous values" are simply SVs.

Re^2: eval order of args to a sub
by graq (Curate) on May 31, 2007 at 07:25 UTC
    Can you guess what the following prints?
    perl -le"$i=3; sub { $_1++; print @_ }->($i+0, ++$i, $i+0, $i);"

    That's a sneaky one! I was completely wrong:
    syntax error at -e line 1, near "="

    PS: Just joking with you :)

    -=( Graq )=-

Re^2: eval order of args to a sub
by eXile (Priest) on May 31, 2007 at 13:56 UTC
    thanks for that explanation, I experimented a bit with that behaviour and found this a fun example:
    perl -le'$i=3; print($i, $i++, $i);'
    434
    perl -le'$i=3; print($i, $i+1, $i);'
    343
    
Re^2: eval order of args to a sub
by mrpeabody (Friar) on Jun 03, 2007 at 09:55 UTC
    Further,
    $ perl -e'$i=3; sub { $_[0]++; print @_ }->(++$i, $i+0);' 54 $ perl -e'$i=3; sub { $_[1]++; print @_ }->($i+0, ++$i);' 35

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-25 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found