Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: regexp list return 5.6 vs 5.8

by Sixtease (Friar)
on Jan 24, 2008 at 11:37 UTC ( #663990=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

Very good points. I'll try to be more disciplined with test.

What about this one:

perl -le 'sub x { return qw(a b c);}; $r=x; print $r' c

This returns the plain list and that's what I want to mimic having it in an array. Arrays and list assignments have special meanings in scalar context. Slices do not. I don't know excactly where but it is documented. (I mean, it's documented that arrays and list assignments have the special scalar-context behavior, not that slices don't :-))

Update: From perldata:

If you evaluate an array in scalar context, it returns the length of the array. (Note that this is not true of lists, which return the last value, like the C comma operator
List assignment in scalar context returns the number of elements produced by the expression on the right side of the assignment:

And, as you quoted from perlsub:

A "return" statement may be used to exit a subroutine, optionally specifying the returned value, which will be evaluated in the appropriate context
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:10 UTC
    It is stated in perldata - in parens!
    Assignment is a little bit special in that it uses its left argument to determine the context for the right argument. Assignment to a scalar evaluates the right-hand side in scalar context, while assignment to an array or hash evaluates the righthand side in list context. Assignment to a list (or slice, which is just a list anyway) also evaluates the righthand side in list context.

    Then there's perlop which states that qw generates a real list at compile time, and returns the last element of that in scalar context (which AFAIK is resolved at runtime). Array slices seem to work the same way (compile time list construction).

    But that either

    • shouldn't be the case with subroutine returns, according to the docs, or
    • should be documented in the docs, like, "Subroutines return lists, except when they don't. Aggregates are returned as flattened lists, but lists are evaluated in the caller's context".

    Subroutine return values are evaluated in the caller's context on return, they are not made into a list context on exit which is then evaluated in the caller's context. This is arguably a feature...

    Thanks for bringing that up.

    --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}
Re^4: regexp list return 5.6 vs 5.8
by hipowls (Curate) on Jan 24, 2008 at 11:43 UTC

    Works for me on both ancient versions of perl;-)

Log In?
Username:
Password:

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

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












    Results (209 votes). Check out past polls.

    Notices?