.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.48) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Required to disable full justification in groff 1.23.0. .if n .ds AD l .\" ======================================================================== .\" .IX Title "Sub::Uplevel 3" .TH Sub::Uplevel 3 2026-10-26 "perl v5.44.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Sub::Uplevel; \& \& sub foo { \& print join " \- ", caller; \& } \& \& sub bar { \& uplevel 1, \e&foo; \& } \& \& #line 11 \& bar(); # main \- foo.plx \- 11 .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Like Tcl\*(Aqs \fBuplevel()\fR function, but not quite so dangerous. The idea is just to fool \fBcaller()\fR. All the really naughty bits of Tcl\*(Aqs \&\fBuplevel()\fR are avoided. .PP \&\fBTHIS IS NOT THE SORT OF THING YOU WANT TO DO EVERYDAY\fR .IP \fBuplevel\fR 4 .IX Item "uplevel" .Vb 1 \& uplevel $num_frames, \e&func, @args; .Ve .Sp Makes the given function think it\*(Aqs being executed \f(CW$num_frames\fR higher than the current stack level. So when they use caller($frames) it will actually give caller($frames + \f(CW$num_frames\fR) for them. .Sp \&\f(CW\*(C`uplevel(1, \e&some_func, @_)\*(C'\fR is effectively \f(CW\*(C`goto &some_func\*(C'\fR but you don\*(Aqt immediately exit the current subroutine. So while you can\*(Aqt do this: .Sp .Vb 5 \& sub wrapper { \& print "Before\en"; \& goto &some_func; \& print "After\en"; \& } .Ve .Sp you can do this: .Sp .Vb 6 \& sub wrapper { \& print "Before\en"; \& my @out = uplevel 1, &some_func; \& print "After\en"; \& return @out; \& } .Ve .Sp \&\f(CW\*(C`uplevel\*(C'\fR has the ability to issue a warning if \f(CW$num_frames\fR is more than the current call stack depth, although this warning is disabled and compiled out by default as the check is relatively expensive. .Sp To enable the check for debugging or testing, you should set the global \&\f(CW$Sub::Uplevel::CHECK_FRAMES\fR to true before loading Sub::Uplevel for the first time as follows: .Sp .Vb 1 \& #!/usr/bin/perl \& \& BEGIN { \& $Sub::Uplevel::CHECK_FRAMES = 1; \& } \& use Sub::Uplevel; .Ve .Sp Setting or changing the global after the module has been loaded will have no effect. .SH EXAMPLE .IX Header "EXAMPLE" The main reason I wrote this module is so I could write wrappers around functions and they wouldn\*(Aqt be aware they\*(Aqve been wrapped. .PP .Vb 1 \& use Sub::Uplevel; \& \& my $original_foo = \e&foo; \& \& *foo = sub { \& my @output = uplevel 1, $original_foo; \& print "foo() returned: @output"; \& return @output; \& }; .Ve .PP If this code frightens you \fByou should not use this module.\fR .SH "BUGS and CAVEATS" .IX Header "BUGS and CAVEATS" Well, the bad news is \fBuplevel()\fR is about 5 times slower than a normal function call. XS implementation anyone? It also slows down every invocation of \fBcaller()\fR, regardless of whether \fBuplevel()\fR is in effect. .PP Sub::Uplevel overrides CORE::GLOBAL::caller temporarily for the scope of each uplevel call. It does its best to work with any previously existing CORE::GLOBAL::caller (both when Sub::Uplevel is first loaded and within each uplevel call) such as from Contextual::Return or Hook::LexWrap. .PP However, if you are routinely using multiple modules that override CORE::GLOBAL::caller, you are probably asking for trouble. .PP You \fBshould\fR load Sub::Uplevel as early as possible within your program. As with all CORE::GLOBAL overloading, the overload will not affect modules that have already been compiled prior to the overload. One module that often is unavoidably loaded prior to Sub::Uplevel is Exporter. To forcibly recompile Exporter (and Exporter::Heavy) after loading Sub::Uplevel, use it with the ":aggressive" tag: .PP .Vb 1 \& use Sub::Uplevel qw/:aggressive/; .Ve .PP The private function \f(CWSub::Uplevel::_force_reload()\fR may be passed a list of additional modules to reload if ":aggressive" is not aggressive enough. Reloading modules may break things, so only use this as a last resort. .PP As of version 0.20, Sub::Uplevel requires Perl 5.6 or greater. .SH HISTORY .IX Header "HISTORY" Those who do not learn from HISTORY are doomed to repeat it. .PP The lesson here is simple: Don\*(Aqt sit next to a Tcl programmer at the dinner table. .SH THANKS .IX Header "THANKS" Thanks to Brent Welch, Damian Conway and Robin Houston. .PP See http://www.perl.com/perl/misc/Artistic.html .SH "SEE ALSO" .IX Header "SEE ALSO" PadWalker (for the similar idea with lexicals), Hook::LexWrap, Tcl\*(Aqs \fBuplevel()\fR at http://www.scriptics.com/man/tcl8.4/TclCmd/uplevel.htm