Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Perl: porters

bug? delete local $hash{element}

 

 

Perl porters RSS feed   Index | Next | Previous | View Threaded


perl.p5p at rjbs

May 18, 2008, 2:10 PM

Post #1 of 3 (121 views)
Permalink
bug? delete local $hash{element}

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
samples:


$ 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)};

--
rjbs


rgarciasuarez at gmail

May 18, 2008, 2:20 PM

Post #2 of 3 (113 views)
Permalink
Re: bug? delete local $hash{element} [In reply to]

2008/5/18 Ricardo SIGNES <perl.p5p[at]rjbs.manxome.org>:
> What I had tried, and did not work, was:
>
> delete local $hash{a};

That's an interesting point of syntax. Are there any other idioms
where local is used as an lvalue ? I don't this this should be too
hard to make work.

> Also not working, but should be working:
>
> delete @hash{qw(a b c)};

What do you mean, it doesn't work ? With a local ?


perl.p5p at rjbs

May 18, 2008, 3:32 PM

Post #3 of 3 (108 views)
Permalink
Re: bug? delete local $hash{element} [In reply to]

* Rafael Garcia-Suarez <rgarciasuarez[at]gmail.com> [2008-05-18T17:20:01]
> 2008/5/18 Ricardo SIGNES <perl.p5p[at]rjbs.manxome.org>:
> > What I had tried, and did not work, was:
> >
> > delete local $hash{a};
>
> That's an interesting point of syntax. Are there any other idioms
> where local is used as an lvalue ? I don't this this should be too
> hard to make work.

Since localization undefs, the only things that seem like they'd be useful (off
the top of my head) are assignment and deletion. substr wouldn't be useful.
Nothing that expects a reference would be useful either.

How about: routine(local $hash{element}); sub routine { $_[0] = 10 };

I expect that would work... and it does.

> > Also not working, but should be working:
> >
> > delete @hash{qw(a b c)};
>
> What do you mean, it doesn't work ? With a local ?

Sorry, yes. delete local @hash{qw(a b c)}

--
rjbs

Perl porters RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.