use strict; use warnings; use feature 'say'; package Foo { sub new { my $pkg = shift; return bless {}, $pkg } sub frob { my $self = shift; $self-> foo( \&cb ) } sub blurt { my $self = shift; $self-> foo( 'cb' ) } sub foo { my ( $self, $cb ) = @_; $self-> $cb } sub cb { my $self = shift; say ref $self, ' ', __PACKAGE__ } }; package Bar { our @ISA = ( 'Foo' ); sub cb { my $self = shift; say ref $self, ' ', __PACKAGE__ } }; my $foo = Foo-> new; my $bar = Bar-> new; $foo-> frob; $bar-> frob; $foo-> blurt; $bar-> blurt;

It says (which doesn't require explanation, that's not the question):

Foo Foo Bar Foo Foo Foo Bar Bar

The callbacks in above classes look the same for the SSCCE, but let's pretend they are different, and I only want to change callback behaviour in derived class.

In practice, there are multiple changes, including some of the "callbacks". My class inherits from CAM::PDF, one of its "cornerstones" is the traverse method, which is called by a few specialized methods with a callback as an argument. e.g., verbatim, a short one:

sub setObjNum { my $self = shift; my $objnode = shift; my $objnum = shift; my $gennum = shift; $self->traverse(0, $objnode, \&_setObjNumCB, [$objnum, $gennum]); return; }
and relevant line in traverse is:
$self->$func($objnode, $funcdata);

Is calling a callback i.e. code reference as method on an instance the accepted and OK practice, to begin with? In derived class, I'm now in some mess of a situation.

Should I override every "specialized" method (even those I didn't intend to) and replace coderefs with strings? Will it hurt performance, and how much?

Maybe I can fix all that at the "central location" (i.e. traverse) without touching every "specialized method" -- to convert a coderef to sub name? How to do that using Perl built-in (or core) functions? Would this solution be better (faster, cleaner, etc.)?


In reply to Methods, within a class, pass callbacks; how to deal with that in derived class? by Anonymous Monk

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.