1Sub::Uplevel(3)       User Contributed Perl Documentation      Sub::Uplevel(3)
2
3
4

NAME

6       Sub::Uplevel - apparently run a function in a higher stack frame
7

SYNOPSIS

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

DESCRIPTION

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

EXAMPLE

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

BUGS and CAVEATS

76       Well, the bad news is uplevel() is about 5 times slower than a normal
77       function call.  XS implementation anyone?
78
79       Sub::Uplevel overrides CORE::GLOBAL::caller temporarily for the scope
80       of each uplevel call.  It does its best to work with any previously
81       existing CORE::GLOBAL::caller (both when Sub::Uplevel is first loaded
82       and within each uplevel call) such as from Contextual::Return or
83       Hook::LexWrap.
84
85       However, if you are routinely using multiple modules that override
86       CORE::GLOBAL::caller, you are probably asking for trouble.
87
88       As of version 0.20, Sub::Uplevel requires Perl 5.6 or greater.
89

HISTORY

91       Those who do not learn from HISTORY are doomed to repeat it.
92
93       The lesson here is simple:  Don't sit next to a Tcl programmer at the
94       dinner table.
95

THANKS

97       Thanks to Brent Welch, Damian Conway and Robin Houston.
98

AUTHORS

100       David A Golden <dagolden@cpan.org> (current maintainer)
101
102       Michael G Schwern <schwern@pobox.com> (original author)
103

LICENSE

105       Original code Copyright (c) 2001 to 2007 by Michael G Schwern.
106       Additional code Copyright (c) 2006 to 2008 by David A Golden.
107
108       This program is free software; you can redistribute it and/or modify it
109       under the same terms as Perl itself.
110
111       See http://www.perl.com/perl/misc/Artistic.html
112

SEE ALSO

114       PadWalker (for the similar idea with lexicals), Hook::LexWrap, Tcl's
115       uplevel() at http://www.scriptics.com/man/tcl8.4/TclCmd/uplevel.htm
116
117
118
119perl v5.10.1                      2010-11-12                   Sub::Uplevel(3)
Impressum