note
jbert
If you want a goto because you don't want to continue in your existing function any more, then that means you want a return:
<c>
return $self->myFunction;
</c>
If you really do want a code ref which allows you to make use of $self, then you can create one with <c>sub</c>:
<c>
# Pick a method, wrap it up an a coderef and return it
sub method_chooser {
my $self = shift;
return (rand % 2) : ? sub { $self->runAlice; } : sub { $self->runCharlie };
}
sub do_stuff {
my $self = shift;
# Dunno what the chooser will choose
my $code_ref = $self->method_chooser();
# But run it all the same
$code_ref->();
}
</c>
Note that <c>method_chooser</c> will return a coderef which stores ('closes over') the value of <c>$self</c>. i.e. it will keep a reference to your object. This is handy, but it obviously counts as a reference for reference counting purposes, i.e. you can get a ref loop that way (and the consequent memory leak).
722627
722627