views:

70

answers:

2

I have run across the following pattern a few times while developing Perl modules that use AUTOLOAD or other subroutine dispatch techniques:

sub AUTOLOAD {
    my $self = $_[0];

    my $code = $self->figure_out_code_ref( $AUTOLOAD );

    goto &$code;
}

This works fine, and caller sees the correct scope.

Now what I would like to do is to locally set $_ equal to $self during the execution of &$code. Which would be something like this:

sub AUTOLOAD {
    my $self = $_[0];

    my $code = $self->figure_out_code_ref( $AUTOLOAD );

    local *_ = \$self;

    # and now the question is how to call &$code

    # goto &$code;  # wont work since local scope changes will 
                    # be unrolled before the goto

    # &$code;  # will preserve the local, but caller will report an
               # additional stack frame  
}

Solutions that involve wrapping caller are not acceptable due to performance and dependency issues. So that seems to rule out the second option.

Moving back to the first, the only way to prevent the new value of $_ from going out of scope during the goto would be to either not localize the change (not a viable option) or to implement some sort of uplevel_local or goto_with_local.

I have played around with all sorts of permutations involving PadWalker, Sub::Uplevel, Scope::Upper, B::Hooks::EndOfScope and others, but have not been able to come up with a robust solution that cleans up $_ at the right time, and does not wrap caller.

Has anyone found a pattern that works in this case?

(the SO question: http://stackoverflow.com/questions/200578/how-can-i-localize-perl-variables-in-a-different-stack-frame is related, but preserving caller was not a requirement, and ultimately the answer there was to use a different approach, so that solution is not helpful in this case)

+1  A: 

Sub::Uplevel appears to work -- at least for a simple case not involving AUTOLOAD:

use strict;
use warnings;
use Sub::Uplevel;

$_ = 1;
bar();

sub foo {
    printf  "%s %s %d - %s\n", caller, $_
}

sub bar {
    my $code = \&foo;
    my $x    = 2;
    local *_ = \$x;
    uplevel 1, $code;
}

The output is:

main c:\temp\foo.pl 6 - 2

Granted, this doesn't really localize a variable in the parent scope, but I don't think you would really want to do that even if you could. You only want to localize $_ for the duration of the call.

Michael Carman
`Sub::Uplevel` will do the job of tricking `caller` but I am not crazy about the way it goes about it (wrapping and localizing `CORE::GLOBAL::caller`). See the BUGS/CAVEATS section of `Sub::Uplevel` for more details.
Eric Strom
Ah, I (mis)read your question as "Sub::Uplevel didn't work" rather than as that you'd disqualified it due to how it worked.
Michael Carman
+1  A: 

The perlfunc documentation for goto points out (emphasis added)

The goto-&NAME form is quite different from the other forms of "goto". In fact, it isn't a goto in the normal sense at all, and doesn't have the stigma associated with other gotos. Instead, it exits the current subroutine (losing any changes set by local)

What sorts of performance concerns allow for indirection through autoloading but not through a wrapper?

Greg Bacon
my concerns about performance are more for the code that I am not writing (users of the module). I can't say if they will be using `caller` or code that uses `caller` extensively, where the performance impact of going from a C builtin to a userland sub might be significant. Since the ultimate goal is just the syntactic nicety of having `$_` set for you, its hard to justify changes to the `CORE::GLOBAL` namespace which might cause performance regressions.
Eric Strom
Eric Strom