I think the perlish syntax you're looking for is a closure/code reference. That is, instead of passing in a full object or type, you pass in a CODE reference that does whatever you need it to do. That code reference, being a closure, would have access to the calling object to do work with it, but any work done on the calling object would still live inside the calling object. That is, the code dealing with $foo would live in Foo.pm, not in Bar.pm.

package Foo; sub do_it { my $self = shift; my $do_it_to = shift; $do_it_to->handler(sub { $self->logger(@_) }); } # and other stuff, like sub logger. package Bar; sub handler { my $self = shift; my $callback = shift; for ($self->get_all_items()) { $callback->($_); } }
Something like that.

If you find yourself needing more than one callback in a single function (such as handler above), you may be better off defining an API that the function needs, and requiring that an object that handles that API be passed in. Otherwise the function call will get ugly.


In reply to Re: A caller() by any other name by Tanktalus
in thread A caller() by any other name by Bloodnok

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.