in reply to Tie::Hash::Stack
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; # ...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Tie::Hash::Stack
by Masem (Monsignor) on Jun 20, 2001 at 16:07 UTC |