develooper Front page | perl.perl5.porters | Postings from April 2003

Re: [perl #22027] undef appears as scalar return value for a list

Thread Previous | Thread Next
From:
Graham Barr
Date:
April 25, 2003 09:42
Subject:
Re: [perl #22027] undef appears as scalar return value for a list
Message ID:
20030425174218.V17850@pobox.com
On Fri, Apr 25, 2003 at 12:08:33AM -0400, Rick Delaney wrote:
> On Thu, Apr 24, 2003 at 10:48:36PM -0400, Mark Jason Dominus wrote:
> > Rick Delaney <rick.delaney@rogers.com>:
> > > 
> > > Should these be different?
> > > 
> > >     my $rv2 = ('a', 'b', 'c', f());
> > >     my $rv2 = ('a', 'b', 'c', );
> > >     sub f { () }
> > 
> > Yes.
> > 
> > This
> > 
> >      my $rv2 = ('a', 'b', 'c', f());
> >      sub f { () }
> > 
> > is identical to this:
> > 
> >      my $rv2 = ('a', 'b', 'c', f());
> >      sub f { }
> > 
> > because the empty parentheses don't matter.
> 
> They're not identical at the opcode level because the parentheses do
> matter, since they create a stub expression that returns undef in scalar
> context.

Right. A list in a scalar context evaluates each element in a scalar context
before any flatening is done. 

sub f { warn wantarray ? 1 : 0; return }
@a = ('a', f(),'');
$a = ('a', f(),'');
__END__
1 at - line 1.
0 at - line 1.

() in a scalar context yeilds undef.

Graham.

Thread Previous | 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