baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:
I have a problem with passing a class method. Let me give an example:
Question in code (there are probably typos but I just need an example so I can study it).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"; }
I could just do :
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)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"; }
Thank you !
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a class method
by haukex (Archbishop) on Feb 10, 2018 at 15:35 UTC | |
by baxy77bax (Deacon) on Feb 10, 2018 at 16:04 UTC | |
by haukex (Archbishop) on Feb 10, 2018 at 17:47 UTC | |
|
Re: Passing a class method
by karlgoethebier (Abbot) on Feb 10, 2018 at 15:49 UTC |