Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

A list returns its last, an array returns its weight

by princepawn (Parson)
on Nov 12, 2002 at 19:05 UTC ( #212361=perlmeditation: print w/ replies, xml ) Need Help??

In scalar context, a sub returning an array returns its "weight" or number of elements, while a sub returning a list returns its "last" or last element:
sub x { (1 , 2 , 7); } sub z { my @L = (1 , 2 , 7); @L } my $x = x; warn $x; my $z = z; warn $z;

This can be useful when you have a sub that was written to return a scalar but you need to return more from it: just return a list and have the last element be what it used to return and put all the new things you want to return from it in a list in front of it:

sub x { ($new_val, $new_val2, $old_value_returned_when_called_in_scalar_cont +ext);

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

update (broquaint): shortened title (was A list returns its last, an array returns its weight (keeping a subroutine which used to return a scalar useable when you need to return more))

Comment on A list returns its last, an array returns its weight
Select or Download Code
dup
by NodeReaper (Curate) on Nov 12, 2002 at 19:14 UTC

    Reason: zigdon please delete - duplicate.

    For more information on this node visit: this

(z) Re: A list returns its last, an array returns its weight ...
by zigdon (Deacon) on Nov 12, 2002 at 19:15 UTC
Re: A list returns its last, an array returns its weight ...
by broquaint (Abbot) on Nov 12, 2002 at 19:17 UTC
    Lists and arrays do this in the rest of nature
    print scalar qw( foo bar baz ),$/; print scalar @{[qw( foo bar baz )]}, $/; __output__ baz 3
    And rather interestingly (to me at least) arrays aren't flattened in lists when evaluated in a scalar context e.g
    print scalar +(undef,@INC),$/; __output__ 10

    HTH

    _________
    broquaint

      The lesson, of course, is that context propagates. It propagates into subroutine calls, down to the point where the sub would return; and it propagates into lists of all kinds.
      @a = qw( x y ); # scalar(@a) == 2 @b = qw( foo bar quux ); print $b[ 0, @a ]; # prints "quux"
      (For those of you who are more familiar with CSS, it could be said that context "cascades". :-)

      jdporter
      ...porque es dificil estar guapo y blanco.

        Hi,

        Indeed, and this is where wantarray comes in.


        ---------------------------
        Dr. Mark Ceulemans
        Senior Consultant
        IT Masters, Belgium

Re: A list returns its last, an array returns its weight
by japhy (Canon) on Nov 12, 2002 at 21:58 UTC
    I've said it before and I'll say it again. There is no such thing as a list in scalar context. If I ask you "what does this function return?" and show you sub foo { (1,10,100) }, you can't be sure. Maybe it returns a list of three numbers, or perhaps it returns the number 100. In scalar context, the comma operator evaluates its left operand, discards the result, and returns the right operand. No list involved.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      So lists in scalar context are really just reduced to a list of expressions evaluated with the comma operator e.g
      sub with_parens { ('foo', 'bar', 'baz') } # is equivalent to the following in scalar context sub without_parens { 'foo', 'bar', 'baz' } print scalar with_parens(), $/; print scalar without_parens(), $/; __output__ baz baz
      Or am I off the mark here?

      _________
      broquaint

        Well, it's that lists don't exist in scalar context.

        In scalar context, a comma-separated group of expressions is a bunch of comma operators doing their thing. In list context, a comma-separated group of expressions is a list.

        _____________________________________________________
        Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
        s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-01 21:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (17 votes), past polls