Running this program:#!/usr/bin/perl -w use strict; use Data::Dumper; $Data::Dumper::Indent = 1; my $hashref; for my $i0 (1..2) { for my $i1 (1..2) { for my $i2 (1..2) { $hashref->{"$i0"}{"$i0:$i1"}{"$i0:$i1:$i2"} = "$i0:$i1:$i2"; } } } deletehashlevel($hashref, 1); print Dumper($hashref); exit; sub athashleveldo { my ($h, $n, $action) = @_; if ($n==0) { $action->($h); } else { athashleveldo($_, $n-1, $action) for values %$h; } } sub deletehashlevel { my ($h, $n) = @_; athashleveldo($h, $n, sub { my %tmp; for my $key (keys %$h) { my $val = delete $h->{$key}; @tmp{keys %$val} = values %$val; } %$h = %tmp; }); } __END__
The error goes away when I apply the same solution as js1 did, replacing:$ ./testhashdel Use of freed value in iteration at ./testhashdel line 27.
withathashleveldo($_, $n-1, $action) for values %$h;
Is this a bug in Perl, lurking to bite me at some later time?my @v = values %$h; athashleveldo($_, $n-1, $action) for @v;
In reply to Re^3: Use of freed value in iteration
by kwetal
in thread Use of freed value in iteration
by js1
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |