+ will rightfully set $num to 26.
+
+ "context_info"
+ my ($package, $filename, $line, $subroutine, $hasargs,
+ $wantarray, $evaltext, $is_require, $hints, $bitmask,
+ $hinthash) = context_info $context;
+
+ Gives information about the context denoted by $context, akin to what
+ "caller" in perlfunc provides but not limited only to subroutine, eval
+ and format contexts. When $context is omitted, it defaults to the
+ current context.
+
+ The returned values are, in order :
+
+ * *(index 0)* : the namespace in use when the context was created ;
+
+ * *(index 1)* : the name of the file at the point where the context
+ was created ;
+
+ * *(index 2)* : the line number at the point where the context was
+ created ;
+
+ * *(index 3)* : the name of the subroutine called for this context, or
+ "undef" if this is not a subroutine context ;
+
+ * *(index 4)* : a boolean indicating whether a new instance of @_ was
+ set up for this context, or "undef" if this is not a subroutine
+ context ;
+
+ * *(index 5)* : the context (in the sense of "wantarray" in perlfunc)
+ in which the context (in our sense) is executed ;
+
+ * *(index 6)* : the contents of the string being compiled for this
+ context, or "undef" if this is not an eval context ;
+
+ * *(index 7)* : a boolean indicating whether this eval context was
+ created by "require", or "undef" if this is not an eval context ;
+
+ * *(index 8)* : the value of the lexical hints in use when the context
+ was created ;
+
+ * *(index 9)* : a bit string representing the warnings in use when the
+ context was created ;
+
+ * *(index 10)* : a reference to the lexical hints hash in use when the
+ context was created (only on perl 5.10 or greater).
+
+ "uplevel"
+ my @ret = uplevel { ...; return @ret };
+ my @ret = uplevel { my @args = @_; ...; return @ret } @args, $context;
+ my @ret = &uplevel($callback, @args, $context);
+
+ Executes the code reference $callback with arguments @args as if it were
+ located at the subroutine stack frame pointed by $context, effectively
+ fooling "caller" and "die" into believing that the call actually
+ happened higher in the stack. The code is executed in the context of the
+ "uplevel" call, and what it returns is returned as-is by "uplevel".
+
+ 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
+
+ Note that if @args is empty, then the $context parameter is optional and
+ defaults to the current context ; otherwise it is mandatory.
+
+ Sub::Uplevel also implements a pure-Perl version of "uplevel". Both are
+ identical, with the following caveats :
+
+ * The Sub::Uplevel implementation of "uplevel" may execute a code
+ reference in the context of any upper stack frame. The Scope::Upper
+ version can only uplevel to a subroutine stack frame, and will croak
+ if you try to target an "eval" or a format.
+
+ * Exceptions thrown from the code called by this version of "uplevel"
+ will not be caught by "eval" blocks between the target frame and the
+ uplevel call, while they will for Sub::Uplevel's version. This means
+ that :
+
+ eval {
+ sub {
+ local $@;
+ eval {
+ sub {
+ uplevel { die 'wut' } CALLER(2); # for Scope::Upper
+ # uplevel(3, sub { die 'wut' }) # for Sub::Uplevel
+ }->();
+ };
+ print "inner block: $@";
+ $@ and exit;
+ }->();
+ };
+ print "outer block: $@";
+
+ will print "inner block: wut..." with Sub::Uplevel and "outer block:
+ wut..." with Scope::Upper.
+
+ * Sub::Uplevel globally overrides the Perl keyword "caller", while
+ Scope::Upper does not.
+
+ A simple wrapper lets you mimic the interface of "uplevel" in
+ Sub::Uplevel :
+
+ use Scope::Upper;
+
+ sub uplevel {
+ my $frame = shift;
+ my $code = shift;
+ my $cxt = Scope::Upper::CALLER($frame);
+ &Scope::Upper::uplevel($code => @_ => $cxt);
+ }
+
+ Albeit the three exceptions listed above, it passes all the tests of
+ Sub::Uplevel.
+
+ "uid"
+ my $uid = uid;
+ my $uid = uid $context;
+
+ Returns an unique identifier (UID) for the context (or dynamic scope)
+ pointed by $context, or for the current context if $context is omitted.
+ This UID will only be valid for the life time of the context it
+ represents, and another UID will be generated next time the same scope
+ is executed.
+
+ 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
+ ...
+ }
+ }
+
+ For example, each loop iteration gets its own UID :
+
+ my %uids;
+
+ for (1 .. 5) {
+ my $uid = uid;
+ $uids{$uid} = $_;
+ }
+
+ # %uids has 5 entries
+
+ The UIDs are not guaranteed to be numbers, so you must use the "eq"
+ operator to compare them.
+
+ To check whether a given UID is valid, you can use the "validate_uid"
+ function.
+
+ "validate_uid"
+ my $is_valid = validate_uid $uid;
+
+ Returns true if and only if $uid is the UID of a currently valid context
+ (that is, it designates a scope that is higher than the current one in
+ the call stack).
+
+ my $uid;
+
+ {
+ $uid = uid();
+ if (validate_uid($uid)) { # yes
+ ...
+ }
+ {
+ if (validate_uid($uid)) { # yes
+ ...
+ }
+ }
+ }
+
+ if (validate_uid($uid)) { # no
+ ...
+ }