develooper Front page | perl.perl5.porters | Postings from October 2016

Re: Extend the fat comma to deal with undef

Thread Previous | Thread Next
From:
David Nicol
Date:
October 10, 2016 07:05
Subject:
Re: Extend the fat comma to deal with undef
Message ID:
CAFwScO_zoQ3pdZELWNcDmgZeZEuFhOqSa=baFPghO0maNP1OVQ@mail.gmail.com
On Thu, Oct 6, 2016 at 6:07 PM, Tony Cook <tony@develop-help.com> wrote:

>
> It doesn't:
>
> $ perl -MData::Dumper -e 'sub x {} %x = ( foo => x() => bar => 1 ); print
> Dumper(\%x)'
> $VAR1 = {
>           'foo' => 'bar',
>           '1' => undef
>         };
>
> => only differs from the comma operator in that it quotes identifiers
> given as its left argument.
>

Drat. Wrapping in scalar works though:

$ perl -MData::Dumper -e 'sub x {} %x = ( foo => scalar(x()) => bar => 1 );
print Dumper(\%x)'
$VAR1 = {
          'bar' => 1,
          'foo' => undef
        };

Also, if you're following the Best Practice of naming all the values prior
to constructing hashes, you can leave things out with the ternary operator
without it being that unclear:
$ perl -MData::Dumper -e 'sub x {} %x = ( ( $foo ? (foo => $foo) : ()), bar
=> 1 ); print Dumper(\%x)'
$VAR1 = {
          'bar' => 1
        };



-- 
"Teaching radical novelties is our main safeguard against dictatorships"
-- Edsger W. Dijkstra

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