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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.