I was trying to generate a sub which relied on the deep binding of a lexical variable, and tried to call $self->SUPER::method() as well:

.... 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.


In reply to Using SUPER in dynamically generated subs by clinton

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.