#!/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. |