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

VERSION

9       This documentation describes version 0.18
10

SYNOPSIS

12         use Sub::Uplevel;
13
14         sub foo {
15             print join " - ", caller;
16         }
17
18         sub bar {
19             uplevel 1, \&foo;
20         }
21
22         #line 11
23         bar();    # main - foo.plx - 11
24

DESCRIPTION

26       Like Tcl's uplevel() function, but not quite so dangerous.  The idea is
27       just to fool caller().  All the really naughty bits of Tcl's uplevel()
28       are avoided.
29
30       THIS IS NOT THE SORT OF THING YOU WANT TO DO EVERYDAY
31
32       uplevel
33             uplevel $num_frames, \&func, @args;
34
35           Makes the given function think it's being executed $num_frames
36           higher than the current stack level.  So when they use call‐
37           er($frames) it will actually give caller($frames + $num_frames) for
38           them.
39
40           "uplevel(1, \&some_func, @_)" is effectively "goto &some_func" but
41           you don't immediately exit the current subroutine.  So while you
42           can't do this:
43
44               sub wrapper {
45                   print "Before\n";
46                   goto &some_func;
47                   print "After\n";
48               }
49
50           you can do this:
51
52               sub wrapper {
53                   print "Before\n";
54                   my @out = uplevel 1, &some_func;
55                   print "After\n";
56                   return @out;
57               }
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

HISTORY

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

THANKS

95       Thanks to Brent Welch, Damian Conway and Robin Houston.
96

AUTHORS

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

LICENSE

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

SEE ALSO

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