X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FScope%2FUpper.pm;h=f2a97c5a5c201c9387d15c0643f5a48e405123f5;hb=0f7334e9f0acbdac38c362be678bd6ecb658cb0b;hp=6ef56c0b9da2dbebed800056b1a5fa8eb28dbe6c;hpb=c6e995c68b5384510061cde2d433e9506a3935f5;p=perl%2Fmodules%2FScope-Upper.git diff --git a/lib/Scope/Upper.pm b/lib/Scope/Upper.pm index 6ef56c0..f2a97c5 100644 --- a/lib/Scope/Upper.pm +++ b/lib/Scope/Upper.pm @@ -128,6 +128,28 @@ L : target('hello'); # "hello from Uplevel::target()" +L and L : + + use Scope::Upper qw; + + my $uid; + + { + $uid = uid(); + { + if ($uid eq uid(UP)) { # yes + ... + } + if (validate_uid($uid)) { # yes + ... + } + } + } + + if (validate_uid($uid)) { # no + ... + } + =head1 DESCRIPTION This module lets you defer actions I that will take place when the control flow returns into an upper scope. @@ -149,7 +171,11 @@ return values immediately to an upper level with L, and know which cont =item * -execute a subroutine in the setting of an upper subroutine stack frame with L. +execute a subroutine in the setting of an upper subroutine stack frame with L ; + +=item * + +uniquely identify contextes with L and L. =back @@ -353,6 +379,73 @@ A simple wrapper lets you mimic the interface of L : Albeit the three exceptions listed above, it passes all the tests of L. +=head2 C + +Returns an unique identifier (UID) for the context (or dynamic scope) pointed by C<$context>, or for the current context if C<$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 C operator to compare them. + +To check whether a given UID is valid, you can use the L function. + +=head2 C + +Returns true if and only if C<$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 + ... + } + =head1 CONSTANTS =head2 C @@ -484,6 +577,7 @@ our %EXPORT_TAGS = ( localize localize_elem localize_delete unwind want_at uplevel + uid validate_uid > ], words => [ qw ], consts => [ qw ],