in reply to Tie::Hash::Stack

Your CLEAR function does too much work, and has a misleading statement.
sub CLEAR { my $self = shift; foreach ( @$self ) { foreach my $key ( keys %$_ ) { delete $_->{ $key } } } $self = [ { } ]; }
I would just write:
sub CLEAR { my $self = shift; @$self = ( {} ); }
or the more terse
sub CLEAR { $_[0] = [ {} ]; }
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.

My other remark is about your push_hash() and pop_hash() functions. Why not prototype them as (\%), and then export them? This will allow you to do:
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; # ...
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.

japhy -- Perl and Regex Hacker

Replies are listed 'Best First'.
Re: Re: Tie::Hash::Stack
by Masem (Monsignor) on Jun 20, 2001 at 16:07 UTC
    Yea, I don't know why I didn't think of 'reversing' the stack, maybe just got too tied up in keeping to the spirit of the stack. When I wrote this, I was more concerned with handling FIRSTKEY/NEXTKEY, since that's the tricky tie part for this class (and some of the docs and examples are misleading on how this works!) In any case, I've updated the code with your suggestions to 0.02.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain