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

In reply to Re: Tie::Hash::Stack by japhy
in thread Tie::Hash::Stack by Masem

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.