develooper Front page | perl.beginners | Postings from March 2002

context of function calls

Thread Next
From:
Dave Storrs
Date:
March 28, 2002 11:13
Subject:
context of function calls
Message ID:
20020328110346.L25360-100000@megazone23.bigpanda.com
Hey folks,

Executive summary:  It looks like, no matter what context foo() is called
in, its arguments are always evaluated in list context.  Is this correct?
I had always understood that:
	- a function's arguments were evaluated in the same context as the
context of the function (hmm...actually, thinking about it, this seems
like it can't be right.  Is it simply always LIST, or can it vary?)

	- a function's context propogated downwards into any functions it
calls.



Full version:

I thought I had a pretty good handle on the whole concept of context, but
I've tripped over it several times recently, so I set up a test:


#!/usr/local/bin/perl -w

use strict;

$\ = "\n";

{print "\$x = bar()";  my $x =  bar();  foo($x);}
{print "\@x = bar()";  my @x =  bar();  foo(@x);}

{print "\$x = foo( bar() )";  my $x =  foo( bar() );}
{print "\@x = foo( bar() )";  my @x =  foo( bar() );}
{print "foo( bar() )";                 foo( bar() );}

sub foo {
    while (@_) {
        my $parm = shift;
        for ($parm) {
            unless (defined) { $parm = 'VOID';  last; }
            $parm = $parm ? 'LIST' : 'SCALAR';
        }
        print "\t$parm";
    }
}

sub bar { return wantarray(); }
print "Done.";
__END__


This outputs what context bar() is called in.  I expected this to output
the following:

########### EXPECTED OUTPUT.  THIS IS NOT WHAT IT GIVES
$x = bar()
    SCALAR
@x = bar()
    LIST
$x = foo( bar() )
    SCALAR
@x = foo( bar() )
    LIST
foo( bar() )
    VOID
Done.
########### /EXPECTED OUTPUT.



Instead, I get this:


$x = bar()
    SCALAR
@x = bar()
    LIST
$x = foo( bar() )
    LIST
@x = foo( bar() )
    LIST
foo( bar() )
    LIST
Done.


Any comments, anyone?


Dave





Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About