Thanks ikegami - that works a treat.

When I benchmarked the three methods, I got this:

Rate super eval orig super 7794/s -- -88% -93% eval 67568/s 767% -- -35% orig 104167/s 1236% 54% --

... where super is using SUPER, eval is ikegami's method, and orig is the one in the original post.

The eval method would be preferable because it checks the @ISA at runtime, while my method, although faster, would only check @ISA at compile time.

UPDATE At ikegami's suggestion, I have added the benchmark code:

# Setup 3 methods find_ids1, find_ids2, find_ids3 ################################################### use SUPER; # Using a code ref extracted at compile time my $orig = $class->can('find_ids'); install_sub($class,'find_ids1', sub { my $class = shift; my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $orig->($class,\%params); }); # Using SUPER install_sub($class,'find_ids2', sub { my $class = shift; my $super = $class->super('find_ids'); my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $super->($class,\%params); }); # Using string eval my $sub = eval "package $class;".<<'SUB'; *find_ids3 = sub { my $class = shift; my %params = ref $_[0] ? %{ $_[0] } : (@_); foreach my $column (keys %params) { if (exists $alias_for->{$column}) { $params{$alias_for->{$column}} = delete $params{$column}; } } return $class->SUPER::find_ids(\%params); }; SUB ############### # Run the Benchmark use Benchmark qw (cmpthese); cmpthese (100000, { orig => sub {MyClass->find_ids1()}, super => sub {MyClass->find_ids2()}, eval => sub {MyClass->find_ids3()}, }); ######################### # The find_ids method in the superclass just returned #########################

In reply to Re^6: Using SUPER in dynamically generated subs by clinton
in thread 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.