.\" Automatically generated by Pod::Man 2.25 (Pod::Simple 3.16) .\" .\" 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 .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' '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 turned on, 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. .ie \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .el \{\ . de IX .. .\} .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Scope::Upper 3pm" .TH Scope::Upper 3pm "2011-10-10" "perl v5.14.2" "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 "NAME" Scope::Upper \- Act on upper scopes. .SH "VERSION" .IX Header "VERSION" Version 0.18 .SH "SYNOPSIS" .IX Header "SYNOPSIS" \&\*(L"reap\*(R", \*(L"localize\*(R", \*(L"localize_elem\*(R", \*(L"localize_delete\*(R" and \*(L"\s-1WORDS\s0\*(R" : .PP .Vb 1 \& package Scope; \& \& use Scope::Upper qw< \& reap localize localize_elem localize_delete \& :words \& >; \& \& sub new { \& my ($class, $name) = @_; \& \& localize \*(Aq$tag\*(Aq => bless({ name => $name }, $class) => UP; \& \& reap { print Scope\->tag\->name, ": end\en" } UP; \& } \& \& # Get the tag stored in the caller namespace \& sub tag { \& my $l = 0; \& my $pkg = _\|_PACKAGE_\|_; \& $pkg = caller $l++ while $pkg eq _\|_PACKAGE_\|_; \& \& no strict \*(Aqrefs\*(Aq; \& ${$pkg . \*(Aq::tag\*(Aq}; \& } \& \& sub name { shift\->{name} } \& \& # Locally capture warnings and reprint them with the name prefixed \& sub catch { \& localize_elem \*(Aq%SIG\*(Aq, \*(Aq_\|_WARN_\|_\*(Aq => sub { \& print Scope\->tag\->name, \*(Aq: \*(Aq, @_; \& } => UP; \& } \& \& # Locally clear @INC \& sub private { \& for (reverse 0 .. $#INC) { \& # First UP is the for loop, second is the sub boundary \& localize_delete \*(Aq@INC\*(Aq, $_ => UP UP; \& } \& } \& \& ... \& \& package UserLand; \& \& { \& Scope\->new("top"); # initializes $UserLand::tag \& \& { \& Scope\->catch; \& my $one = 1 + undef; # prints "top: Use of uninitialized value..." \& \& { \& Scope\->private; \& eval { require Cwd }; \& print $@; # prints "Can\*(Aqt locate Cwd.pm in @INC \& } # (@INC contains:) at..." \& \& require Cwd; # loads Cwd.pm \& } \& \& } # prints "top: done" .Ve .PP \&\*(L"unwind\*(R" and \*(L"want_at\*(R" : .PP .Vb 1 \& package Try; \& \& use Scope::Upper qw; \& \& sub try (&) { \& my @result = shift\->(); \& my $cx = SUB UP; # Point to the sub above this one \& unwind +(want_at($cx) ? @result : scalar @result) => $cx; \& } \& \& ... \& \& sub zap { \& try { \& my @things = qw; \& return @things; # returns to try() and then outside zap() \& # not reached \& }; \& # not reached \& } \& \& my @stuff = zap(); # @stuff contains qw \& my $stuff = zap(); # $stuff contains 3 .Ve .PP \&\*(L"uplevel\*(R" : .PP .Vb 1 \& package Uplevel; \& \& use Scope::Upper qw; \& \& sub target { \& faker(@_); \& } \& \& sub faker { \& uplevel { \& my $sub = (caller 0)[3]; \& print "$_[0] from $sub()"; \& } @_ => CALLER(1); \& } \& \& target(\*(Aqhello\*(Aq); # "hello from Uplevel::target()" .Ve .PP \&\*(L"uid\*(R" and \*(L"validate_uid\*(R" : .PP .Vb 1 \& use Scope::Upper qw; \& \& my $uid; \& \& { \& $uid = uid(); \& { \& if ($uid eq uid(UP)) { # yes \& ... \& } \& if (validate_uid($uid)) { # yes \& ... \& } \& } \& } \& \& if (validate_uid($uid)) { # no \& ... \& } .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module lets you defer actions \fIat run-time\fR that will take place when the control flow returns into an upper scope. Currently, you can: .IP "\(bu" 4 hook an upper scope end with \*(L"reap\*(R" ; .IP "\(bu" 4 localize variables, array/hash values or deletions of elements in higher contexts with respectively \*(L"localize\*(R", \*(L"localize_elem\*(R" and \*(L"localize_delete\*(R" ; .IP "\(bu" 4 return values immediately to an upper level with \*(L"unwind\*(R", and know which context was in use then with \*(L"want_at\*(R" ; .IP "\(bu" 4 execute a subroutine in the setting of an upper subroutine stack frame with \*(L"uplevel\*(R" ; .IP "\(bu" 4 uniquely identify contextes with \*(L"uid\*(R" and \*(L"validate_uid\*(R". .SH "FUNCTIONS" .IX Header "FUNCTIONS" In all those functions, \f(CW$context\fR refers to the target scope. .PP You have to use one or a combination of \*(L"\s-1WORDS\s0\*(R" to build the \f(CW$context\fR passed to these functions. This is needed in order to ensure that the module still works when your program is ran in the debugger. The only thing you can assume is that it is an \fIabsolute\fR indicator of the frame, which means that you can safely store it at some point and use it when needed, and it will still denote the original scope. .ie n .SS """reap $callback, $context""" .el .SS "\f(CWreap $callback, $context\fP" .IX Subsection "reap $callback, $context" Adds a destructor that calls \f(CW$callback\fR (in void context) when the upper scope represented by \f(CW$context\fR ends. .ie n .SS """localize $what, $value, $context""" .el .SS "\f(CWlocalize $what, $value, $context\fP" .IX Subsection "localize $what, $value, $context" Introduces a \f(CW\*(C`local\*(C'\fR delayed to the time of first return into the upper scope denoted by \f(CW$context\fR. \&\f(CW$what\fR can be : .IP "\(bu" 4 A glob, in which case \f(CW$value\fR can either be a glob or a reference. \&\*(L"localize\*(R" follows then the same syntax as \f(CW\*(C`local *x = $value\*(C'\fR. For example, if \f(CW$value\fR is a scalar reference, then the \f(CW\*(C`SCALAR\*(C'\fR slot of the glob will be set to \f(CW$$value\fR \- just like \f(CW\*(C`local *x = \e1\*(C'\fR sets \f(CW$x\fR to \f(CW1\fR. .IP "\(bu" 4 A string beginning with a sigil, representing the symbol to localize and to assign to. If the sigil is \f(CW\*(Aq$\*(Aq\fR, \*(L"localize\*(R" follows the same syntax as \f(CW\*(C`local $x = $value\*(C'\fR, i.e. \f(CW$value\fR isn't dereferenced. For example, .Sp .Vb 1 \& localize \*(Aq$x\*(Aq, \e\*(Aqfoo\*(Aq => HERE; .Ve .Sp will set \f(CW$x\fR to a reference to the string \f(CW\*(Aqfoo\*(Aq\fR. Other sigils (\f(CW\*(Aq@\*(Aq\fR, \f(CW\*(Aq%\*(Aq\fR, \f(CW\*(Aq&\*(Aq\fR and \f(CW\*(Aq*\*(Aq\fR) require \f(CW$value\fR to be a reference of the corresponding type. .Sp When the symbol is given by a string, it is resolved when the actual localization takes place and not when \*(L"localize\*(R" is called. Thus, if the symbol name is not qualified, it will refer to the variable in the package where the localization actually takes place and not in the one where the \*(L"localize\*(R" call was compiled. For example, .Sp .Vb 4 \& { \& package Scope; \& sub new { localize \*(Aq$tag\*(Aq, $_[0] => UP } \& } \& \& { \& package Tool; \& { \& Scope\->new; \& ... \& } \& } .Ve .Sp will localize \f(CW$Tool::tag\fR and not \f(CW$Scope::tag\fR. If you want the other behaviour, you just have to specify \f(CW$what\fR as a glob or a qualified name. .Sp Note that if \f(CW$what\fR is a string denoting a variable that wasn't declared beforehand, the relevant slot will be vivified as needed and won't be deleted from the glob when the localization ends. This situation never arises with \f(CW\*(C`local\*(C'\fR because it only compiles when the localized variable is already declared. Although I believe it shouldn't be a problem as glob slots definedness is pretty much an implementation detail, this behaviour may change in the future if proved harmful. .ie n .SS """localize_elem $what, $key, $value, $context""" .el .SS "\f(CWlocalize_elem $what, $key, $value, $context\fP" .IX Subsection "localize_elem $what, $key, $value, $context" Introduces a \f(CW\*(C`local $what[$key] = $value\*(C'\fR or \f(CW\*(C`local $what{$key} = $value\*(C'\fR delayed to the time of first return into the upper scope denoted by \f(CW$context\fR. Unlike \*(L"localize\*(R", \f(CW$what\fR must be a string and the type of localization is inferred from its sigil. The two only valid types are array and hash ; for anything besides those, \*(L"localize_elem\*(R" will throw an exception. \&\f(CW$key\fR is either an array index or a hash key, depending of which kind of variable you localize. .PP If \f(CW$what\fR is a string pointing to an undeclared variable, the variable will be vivified as soon as the localization occurs and emptied when it ends, although it will still exist in its glob. .ie n .SS """localize_delete $what, $key, $context""" .el .SS "\f(CWlocalize_delete $what, $key, $context\fP" .IX Subsection "localize_delete $what, $key, $context" Introduces the deletion of a variable or an array/hash element delayed to the time of first return into the upper scope denoted by \f(CW$context\fR. \&\f(CW$what\fR can be: .IP "\(bu" 4 A glob, in which case \f(CW$key\fR is ignored and the call is equivalent to \f(CW\*(C`local *x\*(C'\fR. .IP "\(bu" 4 A string beginning with \f(CW\*(Aq@\*(Aq\fR or \f(CW\*(Aq%\*(Aq\fR, for which the call is equivalent to respectiveley \f(CW\*(C`local $a[$key]; delete $a[$key]\*(C'\fR and \f(CW\*(C`local $h{$key}; delete $h{$key}\*(C'\fR. .IP "\(bu" 4 A string beginning with \f(CW\*(Aq&\*(Aq\fR, which more or less does \f(CW\*(C`undef &func\*(C'\fR in the upper scope. It's actually more powerful, as \f(CW&func\fR won't even \f(CW\*(C`exists\*(C'\fR anymore. \&\f(CW$key\fR is ignored. .ie n .SS """unwind @values, $context""" .el .SS "\f(CWunwind @values, $context\fP" .IX Subsection "unwind @values, $context" Returns \f(CW@values\fR \fIfrom\fR the context pointed by \f(CW$context\fR, i.e. from the subroutine, eval or format at or just above \f(CW$context\fR, and immediately restart the program flow at this point \- thus effectively returning to an upper scope. .PP The upper context isn't coerced onto \f(CW@values\fR, which is hence always evaluated in list context. This means that .PP .Vb 5 \& my $num = sub { \& my @a = (\*(Aqa\*(Aq .. \*(Aqz\*(Aq); \& unwind @a => HERE; \& # not reached \& }\->(); .Ve .PP will set \f(CW$num\fR to \f(CW\*(Aqz\*(Aq\fR. You can use \*(L"want_at\*(R" to handle these cases. .ie n .SS """want_at $context""" .el .SS "\f(CWwant_at $context\fP" .IX Subsection "want_at $context" Like \f(CW\*(C`wantarray\*(C'\fR, but for the subroutine/eval/format at or just above \f(CW$context\fR. .PP The previous example can then be \*(L"corrected\*(R" : .PP .Vb 5 \& my $num = sub { \& my @a = (\*(Aqa\*(Aq .. \*(Aqz\*(Aq); \& unwind +(want_at(HERE) ? @a : scalar @a) => HERE; \& # not reached \& }\->(); .Ve .PP will rightfully set \f(CW$num\fR to \f(CW26\fR. .ie n .SS """uplevel $code, @args, $context""" .el .SS "\f(CWuplevel $code, @args, $context\fP" .IX Subsection "uplevel $code, @args, $context" Executes the code reference \f(CW$code\fR with arguments \f(CW@args\fR as if it were located at the subroutine stack frame pointed by \f(CW$context\fR, effectively fooling \f(CW\*(C`caller\*(C'\fR and \f(CW\*(C`die\*(C'\fR into believing that the call actually happened higher in the stack. The code is executed in the context of the \f(CW\*(C`uplevel\*(C'\fR call, and what it returns is returned as-is by \f(CW\*(C`uplevel\*(C'\fR. .PP .Vb 3 \& sub target { \& faker(@_); \& } \& \& sub faker { \& uplevel { \& map { 1 / $_ } @_; \& } @_ => CALLER(1); \& } \& \& my @inverses = target(1, 2, 4); # @inverses contains (0, 0.5, 0.25) \& my $count = target(1, 2, 4); # $count is 3 .Ve .PP Sub::Uplevel also implements a pure-Perl version of \f(CW\*(C`uplevel\*(C'\fR. Both are identical, with the following caveats : .IP "\(bu" 4 The Sub::Uplevel implementation of \f(CW\*(C`uplevel\*(C'\fR may execute a code reference in the context of \fBany\fR upper stack frame. The Scope::Upper version can only uplevel to a \fBsubroutine\fR stack frame, and will croak if you try to target an \f(CW\*(C`eval\*(C'\fR or a format. .IP "\(bu" 4 Exceptions thrown from the code called by this version of \f(CW\*(C`uplevel\*(C'\fR will not be caught by \f(CW\*(C`eval\*(C'\fR blocks between the target frame and the uplevel call, while they will for Sub::Uplevel's version. This means that : .Sp .Vb 10 \& eval { \& sub { \& local $@; \& eval { \& sub { \& uplevel { die \*(Aqwut\*(Aq } CALLER(2); # for Scope::Upper \& # uplevel(3, sub { die \*(Aqwut\*(Aq }) # for Sub::Uplevel \& }\->(); \& }; \& print "inner block: $@"; \& $@ and exit; \& }\->(); \& }; \& print "outer block: $@"; .Ve .Sp will print \*(L"inner block: wut...\*(R" with Sub::Uplevel and \*(L"outer block: wut...\*(R" with Scope::Upper. .IP "\(bu" 4 Sub::Uplevel globally overrides the Perl keyword \f(CW\*(C`caller\*(C'\fR, while Scope::Upper does not. .PP A simple wrapper lets you mimic the interface of \*(L"uplevel\*(R" in Sub::Uplevel : .PP .Vb 1 \& use Scope::Upper; \& \& sub uplevel { \& my $frame = shift; \& my $code = shift; \& my $cxt = Scope::Upper::CALLER($frame); \& &Scope::Upper::uplevel($code => @_ => $cxt); \& } .Ve .PP Albeit the three exceptions listed above, it passes all the tests of Sub::Uplevel. .ie n .SS """uid $context""" .el .SS "\f(CWuid $context\fP" .IX Subsection "uid $context" Returns an unique identifier (\s-1UID\s0) for the context (or dynamic scope) pointed by \f(CW$context\fR, or for the current context if \f(CW$context\fR is omitted. This \s-1UID\s0 will only be valid for the life time of the context it represents, and another \s-1UID\s0 will be generated next time the same scope is executed. .PP .Vb 1 \& my $uid; \& \& { \& $uid = uid; \& if ($uid eq uid()) { # yes, this is the same context \& ... \& } \& { \& if ($uid eq uid()) { # no, we are one scope below \& ... \& } \& if ($uid eq uid(UP)) { # yes, UP points to the same scope as $uid \& ... \& } \& } \& } \& \& # $uid is now invalid \& \& { \& if ($uid eq uid()) { # no, this is another block \& ... \& } \& } .Ve .PP For example, each loop iteration gets its own \s-1UID\s0 : .PP .Vb 1 \& my %uids; \& \& for (1 .. 5) { \& my $uid = uid; \& $uids{$uid} = $_; \& } \& \& # %uids has 5 entries .Ve .PP The UIDs are not guaranteed to be numbers, so you must use the \f(CW\*(C`eq\*(C'\fR operator to compare them. .PP To check whether a given \s-1UID\s0 is valid, you can use the \*(L"validate_uid\*(R" function. .ie n .SS """validate_uid $uid""" .el .SS "\f(CWvalidate_uid $uid\fP" .IX Subsection "validate_uid $uid" Returns true if and only if \f(CW$uid\fR is the \s-1UID\s0 of a currently valid context (that is, it designates a scope that is higher than the current one in the call stack). .PP .Vb 1 \& my $uid; \& \& { \& $uid = uid(); \& if (validate_uid($uid)) { # yes \& ... \& } \& { \& if (validate_uid($uid)) { # yes \& ... \& } \& } \& } \& \& if (validate_uid($uid)) { # no \& ... \& } .Ve .SH "CONSTANTS" .IX Header "CONSTANTS" .ie n .SS """SU_THREADSAFE""" .el .SS "\f(CWSU_THREADSAFE\fP" .IX Subsection "SU_THREADSAFE" True iff the module could have been built when thread-safety features. .SH "WORDS" .IX Header "WORDS" .SS "Constants" .IX Subsection "Constants" \fI\f(CI\*(C`TOP\*(C'\fI\fR .IX Subsection "TOP" .PP Returns the context that currently represents the highest scope. .PP \fI\f(CI\*(C`HERE\*(C'\fI\fR .IX Subsection "HERE" .PP The context of the current scope. .SS "Getting a context from a context" .IX Subsection "Getting a context from a context" For any of those functions, \f(CW$from\fR is expected to be a context. When omitted, it defaults to the the current context. .PP \fI\f(CI\*(C`UP $from\*(C'\fI\fR .IX Subsection "UP $from" .PP The context of the scope just above \f(CW$from\fR. .PP \fI\f(CI\*(C`SUB $from\*(C'\fI\fR .IX Subsection "SUB $from" .PP The context of the closest subroutine above \f(CW$from\fR. Note that \f(CW$from\fR is returned if it is already a subroutine context ; hence \f(CW\*(C`SUB SUB == SUB\*(C'\fR. .PP \fI\f(CI\*(C`EVAL $from\*(C'\fI\fR .IX Subsection "EVAL $from" .PP The context of the closest eval above \f(CW$from\fR. Note that \f(CW$from\fR is returned if it is already an eval context ; hence \f(CW\*(C`EVAL EVAL == EVAL\*(C'\fR. .SS "Getting a context from a level" .IX Subsection "Getting a context from a level" Here, \f(CW$level\fR should denote a number of scopes above the current one. When omitted, it defaults to \f(CW0\fR and those functions return the same context as \*(L"\s-1HERE\s0\*(R". .PP \fI\f(CI\*(C`SCOPE $level\*(C'\fI\fR .IX Subsection "SCOPE $level" .PP The \f(CW$level\fR\-th upper context, regardless of its type. .PP \fI\f(CI\*(C`CALLER $level\*(C'\fI\fR .IX Subsection "CALLER $level" .PP The context of the \f(CW$level\fR\-th upper subroutine/eval/format. It kind of corresponds to the context represented by \f(CW\*(C`caller $level\*(C'\fR, but while e.g. \f(CW\*(C`caller 0\*(C'\fR refers to the caller context, \f(CW\*(C`CALLER 0\*(C'\fR will refer to the top scope in the current context. .SS "Examples" .IX Subsection "Examples" Where \*(L"reap\*(R" fires depending on the \f(CW$cxt\fR : .PP .Vb 10 \& sub { \& eval { \& sub { \& { \& reap \e&cleanup => $cxt; \& ... \& } # $cxt = SCOPE(0) = HERE \& ... \& }\->(); # $cxt = SCOPE(1) = UP = SUB = CALLER(0) \& ... \& }; # $cxt = SCOPE(2) = UP UP = UP SUB = EVAL = CALLER(1) \& ... \& }\->(); # $cxt = SCOPE(3) = SUB UP SUB = SUB EVAL = CALLER(2) \& ... .Ve .PP Where \*(L"localize\*(R", \*(L"localize_elem\*(R" and \*(L"localize_delete\*(R" act depending on the \f(CW$cxt\fR : .PP .Vb 10 \& sub { \& eval { \& sub { \& { \& localize \*(Aq$x\*(Aq => 1 => $cxt; \& # $cxt = SCOPE(0) = HERE \& ... \& } \& # $cxt = SCOPE(1) = UP = SUB = CALLER(0) \& ... \& }\->(); \& # $cxt = SCOPE(2) = UP UP = UP SUB = EVAL = CALLER(1) \& ... \& }; \& # $cxt = SCOPE(3) = SUB UP SUB = SUB EVAL = CALLER(2) \& ... \& }\->(); \& # $cxt = SCOPE(4), UP SUB UP SUB = UP SUB EVAL = UP CALLER(2) = TOP \& ... .Ve .PP Where \*(L"unwind\*(R", \*(L"want_at\*(R" and \*(L"uplevel\*(R" point to depending on the \f(CW$cxt\fR: .PP .Vb 10 \& sub { \& eval { \& sub { \& { \& unwind @things => $cxt; # or uplevel { ... } $cxt; \& ... \& } \& ... \& }\->(); # $cxt = SCOPE(0) = SCOPE(1) = HERE = UP = SUB = CALLER(0) \& ... \& }; # $cxt = SCOPE(2) = UP UP = UP SUB = EVAL = CALLER(1) (*) \& ... \& }\->(); # $cxt = SCOPE(3) = SUB UP SUB = SUB EVAL = CALLER(2) \& ... \& \& # (*) Note that uplevel() will croak if you pass that scope frame, \& # because it cannot target eval scopes. .Ve .SH "EXPORT" .IX Header "EXPORT" The functions \*(L"reap\*(R", \*(L"localize\*(R", \*(L"localize_elem\*(R", \*(L"localize_delete\*(R", \*(L"unwind\*(R", \*(L"want_at\*(R" and \*(L"uplevel\*(R" are only exported on request, either individually or by the tags \f(CW\*(Aq:funcs\*(Aq\fR and \f(CW\*(Aq:all\*(Aq\fR. .PP The constant \*(L"\s-1SU_THREADSAFE\s0\*(R" is also only exported on request, individually or by the tags \f(CW\*(Aq:consts\*(Aq\fR and \f(CW\*(Aq:all\*(Aq\fR. .PP Same goes for the words \*(L"\s-1TOP\s0\*(R", \*(L"\s-1HERE\s0\*(R", \*(L"\s-1UP\s0\*(R", \*(L"\s-1SUB\s0\*(R", \*(L"\s-1EVAL\s0\*(R", \*(L"\s-1SCOPE\s0\*(R" and \*(L"\s-1CALLER\s0\*(R" that are only exported on request, individually or by the tags \f(CW\*(Aq:words\*(Aq\fR and \f(CW\*(Aq:all\*(Aq\fR. .SH "CAVEATS" .IX Header "CAVEATS" Be careful that local variables are restored in the reverse order in which they were localized. Consider those examples: .PP .Vb 10 \& local $x = 0; \& { \& reap sub { print $x } => HERE; \& local $x = 1; \& ... \& } \& # prints \*(Aq0\*(Aq \& ... \& { \& local $x = 1; \& reap sub { $x = 2 } => HERE; \& ... \& } \& # $x is 0 .Ve .PP The first case is \*(L"solved\*(R" by moving the \f(CW\*(C`local\*(C'\fR before the \f(CW\*(C`reap\*(C'\fR, and the second by using \*(L"localize\*(R" instead of \*(L"reap\*(R". .PP The effects of \*(L"reap\*(R", \*(L"localize\*(R" and \*(L"localize_elem\*(R" can't cross \f(CW\*(C`BEGIN\*(C'\fR blocks, hence calling those functions in \f(CW\*(C`import\*(C'\fR is deemed to be useless. This is an hopeless case because \f(CW\*(C`BEGIN\*(C'\fR blocks are executed once while localizing constructs should do their job at each run. However, it's possible to hook the end of the current scope compilation with B::Hooks::EndOfScope. .PP Some rare oddities may still happen when running inside the debugger. It may help to use a perl higher than 5.8.9 or 5.10.0, as they contain some context-related fixes. .PP Calling \f(CW\*(C`goto\*(C'\fR to replace an \*(L"uplevel\*(R"'d code frame does not work : .IP "\(bu" 4 for a \f(CW\*(C`perl\*(C'\fR older than the 5.8 series ; .IP "\(bu" 4 for a \f(CW\*(C`DEBUGGING\*(C'\fR \f(CW\*(C`perl\*(C'\fR run with debugging flags set (as in \f(CW\*(C`perl \-D ...\*(C'\fR) ; .IP "\(bu" 4 when the runloop callback is replaced by another module. .PP In those three cases, \*(L"uplevel\*(R" will look for a \f(CW\*(C`goto &sub\*(C'\fR statement in its callback and, if there is one, throw an exception before executing the code. .PP Moreover, in order to handle \f(CW\*(C`goto\*(C'\fR statements properly, \*(L"uplevel\*(R" currently has to suffer a run-time overhead proportional to the size of the the callback in every case (with a small ratio), and proportional to the size of \fBall\fR the code executed as the result of the \*(L"uplevel\*(R" call (including subroutine calls inside the callback) when a \f(CW\*(C`goto\*(C'\fR statement is found in the \*(L"uplevel\*(R" callback. Despite this shortcoming, this \s-1XS\s0 version of \*(L"uplevel\*(R" should still run way faster than the pure-Perl version from Sub::Uplevel. .SH "DEPENDENCIES" .IX Header "DEPENDENCIES" XSLoader (standard since perl 5.006). .SH "SEE ALSO" .IX Header "SEE ALSO" \&\*(L"local\*(R" in perlfunc, \*(L"Temporary Values via \fIlocal()\fR\*(R" in perlsub. .PP Alias, Hook::Scope, Scope::Guard, Guard. .PP Sub::Uplevel. .PP Continuation::Escape is a thin wrapper around Scope::Upper that gives you a continuation passing style interface to \*(L"unwind\*(R". It's easier to use, but it requires you to have control over the scope where you want to return. .PP Scope::Escape. .SH "AUTHOR" .IX Header "AUTHOR" Vincent Pit, \f(CW\*(C`\*(C'\fR, . .PP You can contact me by mail or on \f(CW\*(C`irc.perl.org\*(C'\fR (vincent). .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests to \f(CW\*(C`bug\-scope\-upper at rt.cpan.org\*(C'\fR, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Scope\-Upper . I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. .SH "SUPPORT" .IX Header "SUPPORT" You can find documentation for this module with the perldoc command. .PP .Vb 1 \& perldoc Scope::Upper .Ve .PP Tests code coverage report is available at http://www.profvince.com/perl/cover/Scope\-Upper . .SH "ACKNOWLEDGEMENTS" .IX Header "ACKNOWLEDGEMENTS" Inspired by Ricardo Signes. .PP Thanks to Shawn M. Moore for motivation. .SH "COPYRIGHT & LICENSE" .IX Header "COPYRIGHT & LICENSE" Copyright 2008,2009,2010,2011 Vincent Pit, all rights reserved. .PP This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.