While query

B

Ben Morrow

Quoth Brian McCauley said:
OK I'd like to address this without digging into at the internals of how
the Perl compiler is implemented and looking only at the observable
behaviour of Perl programs that would behave differently under the two
cases.

require AtExit;

sub foo {
my $msg = shift;
print "+$msg";
AtExit->new(sub{ print "-$msg"});
}

my $q = (foo(1),foo(2),foo(3),foo(4));
print "|";

The "two different comma operators" model predicts '+1-1+2-2+3-3+4|-4'.

The "last element of list" model predicts '+1+2+3+4-1-2-3|-4' or
'+1+2+3+4-3-2-1|-4'

No, it doesn't. A list-in-scalar-context evaluates all its elements but
the last in *void* context, meaning that they get destroyed straight
away (before the next entry in the list is evaluated). As I said, the
two models predict the same behaviour, since context is determined at
compile time, making the distinction irrelevant unless one finds one
model easier to understand than the other. I just find it a little odd
that the perl docs seem to have picked the model that is *not* the way
it is actually implemented: maybe most people (unlike me) find the
'separate operators' model easier?
On Perl 5.8.4 I get '+1-1+2-2+3-3+4|-4'.

Yes, as I'd expect.

Something that puzzles me slightly is that if the above is modified to

sub bar {
(foo(1), foo(2), foo(3), foo(4));
}

my $q = bar;
print '|';

then we *do* get (5.8.2) '+1+2+3+4-3-2-1|-4'... it seems that 'returning
a list from a sub called in scalar context' does something slightly
different from 'evaluating a list in scalar context'; specifically, the
non-terminal elements of the list get scalar rather than void context,
and are destroyed later.

Ben
 
J

Joe Smith

Ben said:
Something that puzzles me slightly is that if the above is modified to

sub bar {
(foo(1), foo(2), foo(3), foo(4));
}

my $q = bar;
print '|';

then we *do* get (5.8.2) '+1+2+3+4-3-2-1|-4'... it seems that 'returning
a list from a sub called in scalar context' does something slightly
different from 'evaluating a list in scalar context'; specifically, the
non-terminal elements of the list get scalar rather than void context,
and are destroyed later.

Yes, that is the behavior I've observed as well.

cat temp.pl
sub foo {
print "in foo(@_), context is ",
defined wantarray ?
(wantarray ? 'list' : 'scalar')
: 'void';
print "\n"; }
@_ = ( foo(10),foo(11),foo(12) ); print "\n";
$_ = ( foo(20),foo(21),foo(22) ); print "\n";
sub bar { ( foo(30),foo(31),foo(32) ); }
$_ = bar;

perl temp.pl
in foo(10), context is list
in foo(11), context is list
in foo(12), context is list

in foo(20), context is void
in foo(21), context is void
in foo(22), context is scalar

in foo(30), context is scalar
in foo(31), context is scalar
in foo(32), context is scalar
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top