> Shouldn't this work?:
> print Dumper(do { delete local @$hashref{ @$bad_keys }, $hashref} ); or this print Dumper(do { delete local @$hashref{ @$bad_keys }; $hashref} );
yes it's surprising.
One needs to remember that local is protecting certain entries and not the whole hash (The hash could be a lexical variable, local wouldn't do anyway)
Since you are returning a reference, you'll see the restored entries again after leaving the block.
consider what's happening without delete
DB<91> use Data::Dump qw/pp/
DB<92> ; {local @hash{qw/log schema/};say pp \%hash}; say pp \%hash
{ a => 1, b => 2, c => 3, log => undef, schema => undef }
{ a => 1, b => 2, c => 3, log => "l", schema => "s" }
you can still have the desired effect by just returning a list or a new anonymous hash
DB<96> say pp do {delete local @hash{qw/log schema/}; %hash }; say
+pp \%hash
("a", 1, "c", 3, "b", 2)
{ a => 1, b => 2, c => 3, log => "l", schema => "s" }
DB<97> say pp do {delete local @hash{qw/log schema/}; +{%hash} }; s
+ay pp \%hash
{ a => 1, b => 2, c => 3 }
{ a => 1, b => 2, c => 3, log => "l", schema => "s" }
DB<98>
if you shy away from the overhead to return a new hash, just do the dumping inside the block.
DB<99> ; {delete local @hash{qw/log schema/};say pp \%hash}; say pp
+\%hash
{ a => 1, b => 2, c => 3 }
{ a => 1, b => 2, c => 3, log => "l", schema => "s" }
DB<100>
HTH! :)
|