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:
Rick Delaney
Date:
April 24, 2003 21:08
Subject:
Re: [perl #22027] undef appears as scalar return value for a list
Message ID:
20030425000833.D25139@cs839290-a.mtth.phub.net.cable.rogers.com
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.

> Analogously, this
> 
>      my $rv2 = ('a', 'b', 'c', ());
> 
> should be identical to this:
> 
>      my $rv2 = ('a', 'b', 'c', );
> 
> because the empty parentheses don't matter.

In both cases, a term is expected after the final comma (but is not
required because of some magic).  In the first case it is given one
so the question is should comma be special-cased to ignore that
particular term?

> > If so, why should the inline expression behave differently from the
> > function call?
> 
> 
> Because a function call returns a value.

As does an expression, including C<()>.

-- 
Rick Delaney
rick.delaney@rogers.com

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