in reply to Calling a subroutine when part of call is a variable Contant

You might also use UNIVERSAL::can(), which might make the OO interface slightly more regular:

c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version: $] \n}; ;; use constant { STUFF => { 'bizz' => 'foe', 'bazz' => 'fie', 'bozz' => 'fee', 'buzz' => 'wiz', }, }; ;; my $obj = Kid->new; ;; my $n = 0; ;; METAMETHOD: for my $meta_method (qw/bizz buzz bazz zotz bozz/) { if (not exists STUFF->{$meta_method}) { print qq{no method corresponds to '$meta_method'}; next METAMETHOD; } ;; my $method = STUFF->{$meta_method}; ;; my $coderef = $obj->can($method); if (not $coderef) { print qq{cannot '$meta_method'->'$method' on }, ref $obj; next METAMETHOD; } $coderef->($obj, $method, $n++); } ;; { package Dad; ;; sub new { my $class = shift; return bless \my $x => $class; } sub fee { my $self = shift; $self->all(@_); } sub all { my $self = shift; print qq{method '$_[0]', n == $n}; } } ;; { package Kid; ;; use parent -norequire, qw(Dad); sub new { my $class = shift; return $class->SUPER::new; } sub fie { my $self = shift; $self->all(@_); } sub foe { my $self = shift; $self->all(@_); } } " perl version: 5.008009 method 'foe', n == 1 cannot 'buzz'->'wiz' on Kid method 'fie', n == 2 no method corresponds to 'zotz' method 'fee', n == 3
Note the
    $coderef->($obj, $method, $n++);
syntax for the code reference method call.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: Calling a subroutine when part of call is a variable Contant
by LanX (Saint) on May 27, 2017 at 11:57 UTC
    > Note the 
        $coderef->($obj, $method, $n++); 
    syntax for the code reference method call.

    Side note:

        $obj->$coderef($method, $n++);

    Should do, too. :) 

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Should do ...

      Aha! And indeed it does! (Somehow, I thought the roundabout syntax was necessary...)


      Give a man a fish:  <%-{-{-{-<