in reply to Can caller() or the call stack be tricked?

I used the recommended goto SUB way. Here’s what happens.

There are two programs, in both debug is a fancy little debug message printer. In one of them, _debug is an actual sub that does a little more than this, it also sends the debug message to a server for central logging. In the other app, _debug is just an alias, so any case of _debug will still work: *_debug = \&debug;

Now, in the app that has the actual sub _debug { ... } declared, Perl will tell me ‘Ambiguous use of &{_debug} resolved to &_debug’, for a line which contains the following: goto &{_debug};

What exactly might be so ambiguous about this?


Relevant code pieces for easier understanding:

package OO::SubModule; sub new { my ($class, $args) = @_; my $self = { ( defined $args ? %$args : () ) }; bless($self, $class); ## Some boilerplate debug callback that we can override during const +ruction $self->{debug_cb} //= sub { my $self = shift; carp("DEBUG ".join(", +", map { defined $_ ? $_ : "undef" } @_)); }; return $self; } sub _debug { my $self = $_[0]; goto &{$self->{debug_cb}}; } ## You can say $self->_debug("booya") to do $self->{debug_cb}->("booya +") without it ending up in the call stack
## both apps $oosub = OO::SubModule->new({ debug_cb => sub { my $self = shift; my ($str) = @_; goto &{_debug}; }, }); ## Any occurrence of $self->_debug("booya") within OO::SubModule reall +y does _debug("booya") of the main app, this way ## Without getting in the call stack, right?

Replies are listed 'Best First'.
Re^2: Can caller() or the call stack be tricked?
by Ralesk (Pilgrim) on Aug 06, 2013 at 12:44 UTC

    Sigh. Why did I use curlies in those gotos?