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:
Mike Stok
Date:
April 24, 2003 10:21
Subject:
Re: [perl #22027] undef appears as scalar return value for a list
Message ID:
Pine.LNX.4.44.0304241313100.8054-100000@ratdog.stok.co.uk
On 24 Apr 2003, Rick Delaney wrote:

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

Hence the "I may be being an idiot" comment.  

Thanks,

Mike

-- 
mike@stok.co.uk                    |           The "`Stok' disclaimers" apply.
http://www.stok.co.uk/~mike/       | GPG PGP Key      1024D/059913DA 
mike@exegenix.com                  | Fingerprint      0570 71CD 6790 7C28 3D60
http://www.exegenix.com/           |                  75D2 9EC4 C1C0 0599 13DA


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