I need to supply a callback function to an instance of my class. That function might decide to not handle it and defer to the built-in functionality, after looking at the arguments.

One approach is to have a special return value meaning "not for me".

Another approach, which is more flexible and good for a somewhat different class of problems, is to chain to the original (or earlier) function. The "hard way" is to set up my callback by getting the old value and saving that where my callback can see it, then setting my function as the new one to be called.

But I'm struck with how similar this is, conceptually, to "wrapper methods" and overrides of virtual functions in general. However, I don't want to derive a class to do it, and I want to supply the information per-instance.

But I can't help wondering if a similar mechanism would be useful for per-instance callbacks, either as a full-blown callback manager module, or at least employing these concepts in the design of how callbacks are supplied and possibly chained.

Anyone want to talk about it? Is there a better, modern, way to program callback features?

—John


In reply to chaining callbacks by John M. Dlugosz

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.