2 Sub::Uplevel - apparently run a function in a higher stack frame
5 This documentation describes version 0.1901
11 print join " - ", caller;
19 bar(); # main - foo.plx - 11
22 Like Tcl's uplevel() function, but not quite so dangerous. The idea is
23 just to fool caller(). All the really naughty bits of Tcl's uplevel()
26 THIS IS NOT THE SORT OF THING YOU WANT TO DO EVERYDAY
29 uplevel $num_frames, \&func, @args;
31 Makes the given function think it's being executed $num_frames
32 higher than the current stack level. So when they use
33 caller($frames) it will actually give caller($frames + $num_frames)
36 "uplevel(1, \&some_func, @_)" is effectively "goto &some_func" but
37 you don't immediately exit the current subroutine. So while you
50 my @out = uplevel 1, &some_func;
56 The main reason I wrote this module is so I could write wrappers around
57 functions and they wouldn't be aware they've been wrapped.
61 my $original_foo = \&foo;
64 my @output = uplevel 1, $original_foo;
65 print "foo() returned: @output";
69 If this code frightens you you should not use this module.
72 Well, the bad news is uplevel() is about 5 times slower than a normal
73 function call. XS implementation anyone?
75 Sub::Uplevel overrides CORE::GLOBAL::caller temporarily for the scope of
76 each uplevel call. It does its best to work with any previously existing
77 CORE::GLOBAL::caller (both when Sub::Uplevel is first loaded and within
78 each uplevel call) such as from Contextual::Return or Hook::LexWrap.
80 However, if you are routinely using multiple modules that override
81 CORE::GLOBAL::caller, you are probably asking for trouble.
84 Those who do not learn from HISTORY are doomed to repeat it.
86 The lesson here is simple: Don't sit next to a Tcl programmer at the
90 Thanks to Brent Welch, Damian Conway and Robin Houston.
93 David A Golden <dagolden@cpan.org> (current maintainer)
95 Michael G Schwern <schwern@pobox.com> (original author)
98 Original code Copyright (c) 2001 to 2007 by Michael G Schwern.
99 Additional code Copyright (c) 2006 to 2008 by David A Golden.
101 This program is free software; you can redistribute it and/or modify it
102 under the same terms as Perl itself.
104 See http://www.perl.com/perl/misc/Artistic.html
107 PadWalker (for the similar idea with lexicals), Hook::LexWrap, Tcl's
108 uplevel() at http://www.scriptics.com/man/tcl8.4/TclCmd/uplevel.htm