develooper Front page | perl.perl5.porters | Postings from May 2008

bug? delete local $hash{element}

Thread Next
Ricardo SIGNES
May 18, 2008 14:10
bug? delete local $hash{element}
Message ID:

I lamented on p5p that I couldn't delete a hash element locally, and Nicholas
asked, "Well, what if you localize it and then delete it?"  I hadn't tried
that, so I ended up with this little script:

  use strict;
  use Data::Dumper;

  my %hash = (a => 1, b => 2);
  print Dumper(\%hash);

    local $hash{a} = 4;
    print Dumper(\%hash);
    delete $hash{a};
    print Dumper(\%hash);

  print Dumper(\%hash);

  delete @hash{qw(a b)};
  print Dumper(\%hash);

This behaved just as I'd expect.  What I had tried, and did not work, was:

  delete local $hash{a};

The element got deleted but never restored.  Nicholas produced these two

  $ perl -MO=Concise -e 'delete local $hash{a}'
  7  <@> leave[1 ref] vKP/REFC ->(end)
  1     <0> enter ->2
  2     <;> nextstate(main 1 -e:1) v ->3
  6     <1> delete vK ->7
  -        <1> ex-helem sKM/130 ->6
  4           <1> rv2hv sKR/1 ->5
  3              <$> gv(*hash) s ->4
  5           <$> const(PV "a") s/BARE ->6
  -e syntax OK

  $ perl -MO=Concise -e 'local $hash{a}'
  7  <@> leave[1 ref] vKP/REFC ->(end)
  1     <0> enter ->2
  2     <;> nextstate(main 1 -e:1) v ->3
  6     <2> helem vKM/LVINTRO,2 ->7
  4        <1> rv2hv sKR/1 ->5
  3           <$> gv(*hash) s ->4
  5        <$> const(PVIV "a") s/BARE ->6
  -e syntax OK

  <@Nicholas> the behaviour makes sense given the optree
  <@Nicholas> but I think it might actually be a bug
  <@Nicholas> because as best I can tell the local is ignored complletely

While my understanding of the optree here is pretty limited, I agree.  I don't
see why "local delete $hash{element}" should not be the same as "local
$hash{element}; delete $hash{element}"

Also not working, but should be working:

  delete @hash{qw(a b c)};


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