[Chicago-talk] return values

Randal L. Schwartz merlyn at stonehenge.com
Tue Dec 30 14:13:21 CST 2003

>>>>> "Andy" == Andy Lester <andy at petdance.com> writes:

Andy> Make sure you do NOT do

Andy> my $x = stuff();  # $x now == 2

Andy> because $x is in scalar context, and a list assigned to a scalar returns
Andy> the length of the list.

No, you can't have a "list assigned to a scalar".  The context gets
propogated.  For this example, you'll get "2", but only because the
last expression evaluated is "2" in that list. :)  You'd get "b" in this case:

  sub stuff {
    return ("a", "b");

And if you called it as:

        my ($x) = stuff();

you'd get "a" instead!  Wild, huh.

Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn at stonehenge.com> <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!

More information about the Chicago-talk mailing list