1Sub::Uplevel(3) User Contributed Perl Documentation Sub::Uplevel(3)
2
3
4
6 Sub::Uplevel - apparently run a function in a higher stack frame
7
9 use Sub::Uplevel;
10
11 sub foo {
12 print join " - ", caller;
13 }
14
15 sub bar {
16 uplevel 1, \&foo;
17 }
18
19 #line 11
20 bar(); # main - foo.plx - 11
21
23 Like Tcl's uplevel() function, but not quite so dangerous. The idea is
24 just to fool caller(). All the really naughty bits of Tcl's uplevel()
25 are avoided.
26
27 THIS IS NOT THE SORT OF THING YOU WANT TO DO EVERYDAY
28
29 uplevel
30 uplevel $num_frames, \&func, @args;
31
32 Makes the given function think it's being executed $num_frames
33 higher than the current stack level. So when they use
34 caller($frames) it will actually give caller($frames + $num_frames)
35 for them.
36
37 "uplevel(1, \&some_func, @_)" is effectively "goto &some_func" but
38 you don't immediately exit the current subroutine. So while you
39 can't do this:
40
41 sub wrapper {
42 print "Before\n";
43 goto &some_func;
44 print "After\n";
45 }
46
47 you can do this:
48
49 sub wrapper {
50 print "Before\n";
51 my @out = uplevel 1, &some_func;
52 print "After\n";
53 return @out;
54 }
55
56 "uplevel" will issue a warning if $num_frames is more than the
57 current call stack depth.
58
60 The main reason I wrote this module is so I could write wrappers around
61 functions and they wouldn't be aware they've been wrapped.
62
63 use Sub::Uplevel;
64
65 my $original_foo = \&foo;
66
67 *foo = sub {
68 my @output = uplevel 1, $original_foo;
69 print "foo() returned: @output";
70 return @output;
71 };
72
73 If this code frightens you you should not use this module.
74
76 Well, the bad news is uplevel() is about 5 times slower than a normal
77 function call. XS implementation anyone? It also slows down every
78 invocation of caller(), regardless of whether uplevel() is in effect.
79
80 Sub::Uplevel overrides CORE::GLOBAL::caller temporarily for the scope
81 of each uplevel call. It does its best to work with any previously
82 existing CORE::GLOBAL::caller (both when Sub::Uplevel is first loaded
83 and within each uplevel call) such as from Contextual::Return or
84 Hook::LexWrap.
85
86 However, if you are routinely using multiple modules that override
87 CORE::GLOBAL::caller, you are probably asking for trouble.
88
89 You should load Sub::Uplevel as early as possible within your program.
90 As with all CORE::GLOBAL overloading, the overload will not affect
91 modules that have already been compiled prior to the overload. One
92 module that often is unavoidably loaded prior to Sub::Uplevel is
93 Exporter. To forceably recompile Exporter (and Exporter::Heavy) after
94 loading Sub::Uplevel, use it with the ":aggressive" tag:
95
96 use Sub::Uplevel qw/:aggressive/;
97
98 The private function "Sub::Uplevel::_force_reload()" may be passed a
99 list of additional modules to reload if ":aggressive" is not aggressive
100 enough. Reloading modules may break things, so only use this as a last
101 resort.
102
103 As of version 0.20, Sub::Uplevel requires Perl 5.6 or greater.
104
106 Those who do not learn from HISTORY are doomed to repeat it.
107
108 The lesson here is simple: Don't sit next to a Tcl programmer at the
109 dinner table.
110
112 Thanks to Brent Welch, Damian Conway and Robin Houston.
113
115 David A Golden <dagolden@cpan.org> (current maintainer)
116
117 Michael G Schwern <schwern@pobox.com> (original author)
118
120 Original code Copyright (c) 2001 to 2007 by Michael G Schwern.
121 Additional code Copyright (c) 2006 to 2008 by David A Golden.
122
123 This program is free software; you can redistribute it and/or modify it
124 under the same terms as Perl itself.
125
126 See http://www.perl.com/perl/misc/Artistic.html
127
129 PadWalker (for the similar idea with lexicals), Hook::LexWrap, Tcl's
130 uplevel() at http://www.scriptics.com/man/tcl8.4/TclCmd/uplevel.htm
131
132
133
134perl v5.12.0 2010-05-06 Sub::Uplevel(3)