I would just write:sub CLEAR { my $self = shift; foreach ( @$self ) { foreach my $key ( keys %$_ ) { delete $_->{ $key } } } $self = [ { } ]; }
or the more tersesub CLEAR { my $self = shift; @$self = ( {} ); }
The problem with assigning to just $self is that it doesn't modify the data referred to by $self. And you're taking a long and arduous approach to clearing those hashes.sub CLEAR { $_[0] = [ {} ]; }
Update: Hmm... in retrospect, why not push the new hash to the front of the stack? This would make your FETCH faster, so that it can return as soon as a key-value pair is found. Modify code as needed.use Carp; sub push_hash (\%) { my $href = shift; my $obj = tied %$href; croak "must be a Tie::Hash::Stack tied hash" unless $obj and $obj->isa('Tie::Hash::Stack'); push @$obj, {}; } sub pop_hash (\%) { my $href = shift; my $obj = tied %$href; croak "must be a Tie::Hash::Stack tied hash" unless $obj and $obj->isa('Tie::Hash::Stack'); pop @$obj; } # and in your program... tie my(%stash), 'Tie::Hash::Stack'; # ... push_hash %stash; # ...
In reply to Re: Tie::Hash::Stack
by japhy
in thread Tie::Hash::Stack
by Masem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |