in reply to tying hashes and SUPER

while trying to override methods in Tie::StdHash, i've inadvertently screwed up STORE and FETCH.

On first glance, I'd guess that

eval qq{ sub $_ { notify($_); shift->SUPER::$_ } }
might be better written as something like
eval qq{ sub $_ { my $self = shift; notify("$_"); $self->SUPER::$_ (@_); } }
so that it's explict about passing arguments.

Update: Nope, go with chromatic's approach.

Replies are listed 'Best First'.
Re: Re: tying hashes and SUPER
by chromatic (Archbishop) on Apr 16, 2003 at 20:29 UTC

    Ugh. You need to escape the sigil in $self. I'd rather use a closure to prevent me from having to be so clever:

    for my $method ( qw/ TIEHASH STORE FETCH FIRSTKEY NEXTKEY EXISTS CLEAR DELETE UNTIE DESTROY / ) { no strict 'refs'; my $super = 'SUPER::' . $method; *{ $method } = sub { my $self = shift; notify( $method ); $self->$super( @_ ); }; }

    That's also untested. Mixing SUPER and autogenerated methods makes it a little knotty.

      ahh, much better. i was able to fix the minor bugs in dws's solution, but i like your method better. much more straightforward to muck with the symbol table than to exploit eval

      ~Particle *accelerates*

Re^2: tying hashes and SUPER
by tye (Sage) on Apr 16, 2003 at 20:34 UTC

    You also need to pass @_ into the SUPER method and escape the $ in $self.

                    - tye
Re^2: tying hashes and SUPER
by particle (Vicar) on Apr 16, 2003 at 20:25 UTC

    no luck. if i do as you type, it seems notify is not called. the keys are printed, but no warn is generated. i' guess i'll have to dig a little deeper....

    ~Particle *accelerates*