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 10:10
Subject:
Re: [perl #22027] undef appears as scalar return value for a list
Message ID:
20030424131018.A25139@cs839290-a.mtth.phub.net.cable.rogers.com
On Thu, Apr 24, 2003 at 02:02:50PM -0000, Mike Stok wrote:
> 
> sub show {
>     print defined $_[0] ? $_[0] : '(undef)', "\n";
> }
> 
> my $rv = ('a', 'b', 'c');
> show $rv;
> my $rv2 = ('a', 'b', 'c', ());
> show $rv2;
> 
> I would have expected $rv2 to be 'c' as I thought ('a', 'b', 'c', ()) should
> be flattened to ('a', 'b', 'c').  The warnings indicate that in the first 
> case two values are useless, and in the second three values are useless.

I think it's reasonable for $rv2 to be undef since that's what you get
with

   $rv2 = ();

The number of warnings makes sense to me too, since in the first case
there are 3 comma-separated expressions and in the second there are 4.
Flattening should only happen when you have list context.

I'd expect C's comma operator to behave the same, except C doesn't allow
an empty expression like "()".

> Things like
> 
>   @array = ('a', 'b', 'c', ());         # => ('a', 'b', 'c')
>   $scalar = ('a', 'b', 'c', ())[-1];    # => 'c'
> 
> seem to work as I would expect.

These are both list context so the expression "()" is treated as an
empty list (and flattened).  In the scalar context above the expression
"()" is not a list.

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