develooper Front page | perl.perl5.porters | Postings from January 2021

Re: local %SIG = (%SIG, $signame => ...) not working as expected?

Thread Previous
Ricardo Signes
January 12, 2021 20:47
Re: local %SIG = (%SIG, $signame => ...) not working as expected?
Message ID:
On Tue, Jan 12, 2021, at 3:40 PM, Corwin Brust wrote:
> I wonder if you could be running into an entry in %ENV with a undef
> value?  In that case we could end up with an odd number of
> hash-elements and worse.  Perl will ignore the undef values from
> hashes "exploded" within parens, e.g. within the ... part of @x = (
> ... ).  When we come to assign the resulting list back into a new hash
> we then get "misalignments".

That is incorrect.

use v5.30.0;

my %hash  = (a => undef, b => 1);
my @array = %hash;
my %other = @array;

use Data::Dumper;
print Dumper(\%other);

$VAR1 = {
          'a' => undef,
          'b' => 1

You may be thinking of the common mistake where someone puts a subroutine call into a hash assignment:

%hash = (
  a => subroutine(),
  b => 1,

In that case, subroutine() may return something other than a single element, which could be surprising.  If it returns an even number of elements, it also throws b from the key position to the value position.  In that case, you generally want to put "scalar" before subroutine.

undef is already a scalar value.  It does not cause this problem.

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About