.... my ($class,$alias_for) = @_; install_sub( $class, 'find_ids', sub { my ($class,$params) = @_; do_something_with($alias_for); # $alias_for is a lexical which defined outside this sub # which uses deep binding so that it is available # when this sub is called return $class->SUPER::find_ids($params); }); ....
The problem with this, is that SUPER refers to the @ISA of the class it is COMPILED into, not the class of $self at runtime. (See Overridden Methods)
So my other option was doing this:
eval "package $class; sub find_ids { ..... }";
...but this wouldn't work either, because then $alias for wouldn't be defined, and I'm relying on the deep binding of lexical variables to have $alias_for defined.
UPDATE See Re^4: Using SUPER in dynamically generated subs for correct usage of SUPER with an anonymous sub
diotalevi suggested using the module SUPER, but this also relies on the package at compile time. However the docs in there did point me to perltoot which says:
The can() method, called against that object or class, reports back whether its string argument is a callable method name in that class. In fact, it gives you back a function reference to that method
So this is my final code, which works:
.... my ($class,$alias_for) = @_; # Store the original find_ids here my $original_find_ids = $class->can('find_ids') or die "Super-classes of $class don't define sub find_ids"; install_sub( $class, 'find_ids', sub { my ($class,$params) = @_; # Do something with $alias_for; # Use the original find_ids as a function # $class as its first argument, so it thinks # that it has been called as a method return $original_find_ids->($class,$params); }); ....
It works for me. Is there a better way? Anything I'm missing?
CAVEAT This will not take notice of any changes to @ISA after this sub has been defined.
UPDATE See Re^6: Using SUPER in dynamically generated subs for benchmarks comparing the three methods mentioned in this thread.
|
---|