in reply to oo code ref

It has been said that Perl is a loaded gun pointed right at your foot. If that is true, then Perlmonks is the shooting range. Ask ... and ye SHALL receive.

Now, while the good monks have already shown you how to do what you want, someone else really needs to come along and say -- why do need to do that? Don't do that. Wouldn't this work just as well?

my $result = $self->myFunction();
Why are you using a goto on a ref to a method when you already have a ref to it via the object itself? Inquired monks want to know. :)

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: oo code ref
by almut (Canon) on Nov 10, 2008 at 16:58 UTC
    Why are you using a goto on a ref to a method when you already have a ref to it

    I'm not the OP, so I can only speculate on what the intention might have been...  but maybe the idea was to not mess up the call stack? Compare:

    #!/usr/bin/perl package Foo; use Carp "cluck"; sub new { my $class = shift; return bless { @_ }, $class; } sub myFunction { my $self = shift; cluck $self->{msg}; } sub foo1 { my $self = $_[0]; my $func = $self->can('myFunction'); goto &$func; } sub foo2 { my $self = shift; my $func = sub { $self->myFunction }; goto &$func; } sub foo3 { my $self = shift; $self->myFunction(); } package main; my $obj = Foo->new( msg => "foo" ); $obj->myFunction(); # direct call $obj->foo1(); # indirect, using goto (transparent) $obj->foo2(); # indirect, using goto via closure $obj->foo3(); # indirect, using regular call via $self

    Output:

    foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 40 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 41 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 27 Foo::__ANON__() called at ./722627.pl line 42 foo at ./722627.pl line 16 Foo::myFunction('Foo=HASH(0x63c430)') called at ./722627.pl li +ne 33 Foo::foo3('Foo=HASH(0x63c430)') called at ./722627.pl line 43

    Only the goto variant of foo1() is leaving behind a call stack comparable to the direct call.