in reply to Invoke object methods with a symbolic reference

I don't quite get the compactness of $a->${$a->{f}}('foobar'); but maybe what you are loking for is to store a reference to the foo method instead of its name:

sub new { return bless {f => \&foo}, $_[0]; } # \&foo is a reference to foo()

And then you can call the subroutine (including under use strict) as $a->{f}('foobar'); just note that in this case foo is just a regular function, not a method any more and you either have to remove the shift in the function or pass it $a.

Replies are listed 'Best First'.
Rea: Invoke object methods with a symbolic reference
by baku (Scribe) on Feb 14, 2001 at 19:59 UTC

    You could do the same by generating a closure to pass it $a == $self:

    sub new { my $self = {}; bless $self, $_[0]; $self->{f} = sub { &foo ($self, @_); }; return $self; }

    The only reason for the three-line form with temporary variable is that I can't figure out how else to embed the closure "reference" to the object ($self) inside of an hash inside of the object itself :-)

    (fixed minor typo!)