Hi,

I have a problem with passing a class method. Let me give an example:

package A; sub new { my $class = shift; return bless {}, $class; } sub _exe { my ($self,$a) = shift; $self->$a->(); # probably wrong !! } package B; use base 'A'; sub new { my $class = shift; return bless {}, $class; } sub _what { my ($self,$y) = shift; ## HERE I would like to pass _print() with $y (let it be a string) ## to package A, method _exe() so that it executes ## _print($y) in B.... Is this possible?? } sub _print { my ($self,$x) = shift; print "$x\n"; }
Question in code (there are probably typos but I just need an example so I can study it).

I could just do :

package B; sub new { my $class = shift; return bless {}, $class; } sub _what { my ($self,$y) = shift; $self->_print($y); } sub _print { my ($self,$x) = shift; print "$x\n"; }
But this is not what I am after. How to do this and what is the most elegant way to share methods between classes if they are not in a classical parent child relationship? (note that B inherits from A but now i need to pass a method and a variable for that method from B to A to be executed by A)

Thank you !


In reply to Passing a class method by baxy77bax

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.