Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: regexp list return 5.6 vs 5.8

by Sixtease (Friar)
on Jan 24, 2008 at 12:36 UTC ( #663997=note: print w/replies, xml ) Need Help??


in reply to Re^2: regexp list return 5.6 vs 5.8
in thread regexp list return 5.6 vs 5.8

Something that just crossed my mind and turned out to behave like I didn't expect:

perl -le 'sub x { my @x = qw(a b c); my @y = qw(A B C D); return (@x, +@y)} my $r = x(); print $r' 4

Must say it made me LOL :-)

use strict; use warnings; print "Just Another Perl Hacker\n";

Replies are listed 'Best First'.
Re^4: regexp list return 5.6 vs 5.8
by shmem (Chancellor) on Jan 24, 2008 at 12:48 UTC
    From what we've already discussed I would have expected the outcome:
    • the return list is constructed at compile time.
    • at runtime, the list (@x, @y) is evaluated in the caller's context, so it resolves to the comma operator, returning it's last argument - @y
    • then the array @y is evaluated in the caller's context, which happens to be scalar, hence 4 as a result.

    Pretty much the same as

    perl -le '@x=qw(a b c);@y=qw(a b c d); $f=(@x,@y); print $f' 4

    ;-)

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2020-10-20 07:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (209 votes). Check out past polls.

    Notices?