The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Sub::Uplevel - run a function in a higher stack frame

SYNOPSIS

  use Sub::Uplevel;

  sub foo {
      print join " - ", caller;
  }

  sub bar {
      uplevel 1, \&foo;
  }

  #line 11
  bar();    # main - foo.plx - 11

DESCRIPTION

Like Tcl's uplevel() function, but not quite so dangerous. The idea is to fool caller(). All the other nasty bits of uplevel are unnecessary in Perl.

uplevel
  uplevel $num_frames, \&func, @args;

Makes the given function think it's being executed $num_frames higher than the current stack level. So caller() will be caller($num_frames) for them.

BUGS and CAVEATS

Symbol::Uplevel must be used before any code which uses it.

Well, the bad news is uplevel() is about 5 times slower than a normal function call. XS implementation anyone?

AUTHOR

Michael G Schwern <schwern@pobox.com>

SEE ALSO

PadWalker (for the similar idea with lexicals), Hook::LexWrap, Tcl's uplevel() at http://www.scriptics.com/man/tcl8.4/TclCmd/uplevel.htm