Using scalar() on function return values

Discussion in 'Perl Misc' started by J. Romano, Jul 7, 2004.

  1. J. Romano

    J. Romano Guest

    Hi,

    I am posting this message because I am a little confused on what
    the scalar() function returns when called with functions that return
    lists and/or arrays.

    I know that calling scalar() on a list returns the last element of
    a list, and that calling scalar() on an array returns the number of
    elements in the array, but what if scalar() is called on a list that
    ends with an array?

    To clarify, consider the following code:


    #!/usr/bin/perl -w
    use strict;

    # f1() returns a list
    sub f1
    {
    return ('red', 'green', 'blue', 'yellow');
    }

    # f2() returns an array
    sub f2
    {
    my @array = ('cat', 'dog', 'bird');
    return @array;
    }

    # f3() returns a list with an array
    sub f3
    {
    my @array = ('cat', 'dog', 'bird');
    return ('red', 'green', 'blue', 'yellow', @array);
    }


    print "f1() returns ", scalar( f1() ), "\n";
    print "f2() returns ", scalar( f2() ), "\n";
    print "f3() returns ", scalar( f3() ), "\n";

    __END__


    It's easy to see that scalar( f1() ) returns "yellow" (the last
    element of the list), and that scalar( f2() ) returns 3 (the number of
    elements in its array. But what does scalar( f3() ) return?

    You might think it either returns "bird" (the last element of the
    array) or 7 (the number of elements in the list), but it returns
    neither. In fact, it returns 3, apparently the number of elements in
    the @array. This seems odd to me because it looks like that the
    scalar() call is being called twice: Once to single out the @array,
    and the next time to count the number of elements in the @array.

    Does this sound logical, or is what I'm doing considered bad coding
    practice and should never be used? If someone can clarify why what
    I'm getting is logical, that would be appreciated.

    -- Jean-Luc
     
    J. Romano, Jul 7, 2004
    #1
    1. Advertising

  2. >>>>> "J" == J Romano <> writes:

    J> I am posting this message because I am a little confused on what
    J> the scalar() function returns when called with functions that return
    J> lists and/or arrays.

    If you use scalar, it's *not* returning a list or array. It's
    returning a scalar. The last expression evaluated in the subroutine
    is evaluated in scalar context in this case.

    print "Just another Perl hacker,"; # the original

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Jul 7, 2004
    #2
    1. Advertising

  3. J. Romano wrote:
    >
    > sub f3
    > {
    > my @array = ('cat', 'dog', 'bird');
    > return ('red', 'green', 'blue', 'yellow', @array);
    > }
    >
    > what does scalar( f3() ) return?
    >
    > You might think it either returns "bird" (the last element of the
    > array) or 7 (the number of elements in the list), but it returns
    > neither. In fact, it returns 3, apparently the number of elements
    > in the @array.
    >
    > If someone can clarify why what I'm getting is logical, that would
    > be appreciated.


    Here is an attempt:

    From "perldoc perlsub":
    "The return value of a subroutine is the value of the last expression
    evaluated. More explicitly, a return statement may be used to exit the
    subroutine, optionally specifying the returned value, which will be
    evaluated in the appropriate context (list, scalar, or void) depending
    on the context of the subroutine call."

    Accordingly, your observation is not related to the subroutine, but
    this gives a similar result (together with a bunch of warnings):

    my @array = qw(cat dog bird);
    print scalar ('red', 'green', 'blue', 'yellow', @array);

    From "perldoc perldata":
    "LISTs do automatic interpolation of sublists. That is, when a LIST is
    evaluated, each element of the list is evaluated in list context, and
    the resulting list value is interpolated into LIST just as if each
    individual element were a member of LIST."

    That is true when a LIST is evaluated in list context but, as your
    example illustrates, not when it is evaluated in scalar context.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 7, 2004
    #3
  4. J. Romano

    Joe Smith Guest

    J. Romano wrote:

    > But what does scalar( f3() ) return?


    In list context, f3's return() is in list context, and returns a list.
    In scalar context, f3's return() is evaluated in scalar context, which
    means that each of its arguments is evaluated in scalar context individually.

    return scalar('red', 'green', 'blue', @array);
    is the same as
    return (scalar('red'), scalar('green'), scalar('blue'), scalar(@array));
    which in this case is
    return (scalar('red'), scalar('green'), scalar('blue'), 3);

    -Joe
     
    Joe Smith, Jul 7, 2004
    #4
  5. J. Romano

    J. Romano Guest

    Joe Smith <> wrote in message news:<17PGc.38208$IQ4.25974@attbi_s02>...
    >
    > return scalar('red', 'green', 'blue', @array);
    > is the same as
    > return (scalar('red'), scalar('green'), scalar('blue'), scalar(@array));
    > which in this case is
    > return (scalar('red'), scalar('green'), scalar('blue'), 3);


    Thanks. That explains it well (you're the first to explain it to me that way).

    Thank-you for all your responses.

    -- Jean-Luc
     
    J. Romano, Jul 11, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jm
    Replies:
    2
    Views:
    509
  2. Chris Rebert
    Replies:
    1
    Views:
    726
    Bobby
    May 28, 2009
  3. Clint Olsen
    Replies:
    6
    Views:
    401
    Jeff 'japhy' Pinyan
    Nov 13, 2003
  4. Mark

    Replace scalar in another scalar

    Mark, Jan 27, 2005, in forum: Perl Misc
    Replies:
    4
    Views:
    191
    Arndt Jonasson
    Jan 27, 2005
  5. Willem
    Replies:
    62
    Views:
    663
    Ilya Zakharevich
    Sep 9, 2011
Loading...

Share This Page