X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=lib%2FScope%2FContext.pm;h=8fafca8b5a3e8ed92217088cb4f1a0c7de30ccae;hb=f8d2fd90ffbbe951a24f874b76712694f8cd1f8a;hp=4a45cbb540c61f2b7b60a2d0e2e0d587b07be42d;hpb=311423ea1bdbde2c8dde8ce3a772a1036365ec4f;p=perl%2Fmodules%2FScope-Context.git diff --git a/lib/Scope/Context.pm b/lib/Scope/Context.pm index 4a45cbb..8fafca8 100644 --- a/lib/Scope/Context.pm +++ b/lib/Scope/Context.pm @@ -8,7 +8,7 @@ use warnings; use Carp (); use Scalar::Util (); -use Scope::Upper 0.18 (); +use Scope::Upper 0.21 (); =head1 NAME @@ -92,7 +92,7 @@ will croak when L is called. my $cxt = Scope::Context->new; my $cxt = Scope::Context->new($scope_upper_cxt); -Creates a new immutable L object from the L-comptabile context C<$context>. +Creates a new immutable L object from the L-comptabile context identifier C<$context>. If omitted, C<$context> defaults to the current context. =cut @@ -133,13 +133,13 @@ sub _croak { my $scope_upper_cxt = $cxt->cxt; -Read-only accessor to the L context corresponding to the topic L object. +Read-only accessor to the L context identifier associated with the invocant. =head2 C my $uid = $cxt->uid; -Read-only accessor to the L UID of the topic L object. +Read-only accessor to the L unique identifier representing the L context associated with the invocant. =cut @@ -171,7 +171,7 @@ use overload ( my $is_valid = $cxt->is_valid; -Returns true if and only if the topic context is still valid (that is, it designates a scope that is higher than the topic context in the call stack). +Returns true if and only if the invocant is still valid (that is, it designates a scope that is higher on the call stack than the current scope). =cut @@ -181,7 +181,7 @@ sub is_valid { Scope::Upper::validate_uid($_[0]->uid) } $cxt->assert_valid; -Throws an exception if the topic context has expired and is no longer valid. +Throws an exception if the invocant has expired and is no longer valid. Returns true otherwise. =cut @@ -198,7 +198,7 @@ sub assert_valid { my $want = $cxt->want; -Returns the Perl context (in the sense of C : C for void context, C<''> for scalar context, and true for list context) in which is executed the scope corresponding to the topic L object. +Returns the Perl context (in the sense of C : C for void context, C<''> for scalar context, and true for list context) in which is executed the scope pointed by the invocant. =cut @@ -216,9 +216,9 @@ sub want { my $up_cxt = $cxt->up($frames); my $up_cxt = Scope::Context->up; -Returns a new L object pointing to the C<$frames>-th upper scope above the topic context. +Returns a new L object pointing to the C<$frames>-th upper scope above the scope pointed by the invocant. -This method can also be invoked as a class method, in which case it is equivalent to calling L on a L object for the current context. +This method can also be invoked as a class method, in which case it is equivalent to calling L on a L object representing the current context. If omitted, C<$frames> defaults to C<1>. @@ -236,15 +236,16 @@ If omitted, C<$frames> defaults to C<1>. sub up { my ($self, $frames) = @_; + my $cxt; if (Scalar::Util::blessed($self)) { $self->assert_valid; + $cxt = $self->cxt; } else { - $self = $self->new(Scope::Upper::UP(Scope::Upper::SUB())); + $cxt = Scope::Upper::UP(Scope::Upper::SUB()); } $frames = 1 unless defined $frames; - my $cxt = $self->cxt; $cxt = Scope::Upper::UP($cxt) for 1 .. $frames; $self->new($cxt); @@ -256,11 +257,11 @@ sub up { my $sub_cxt = $cxt->sub($frames); my $sub_cxt = Scope::Context->sub; -Returns a new L object pointing to the C<$frames>-th subroutine scope above the topic context. +Returns a new L object pointing to the C<$frames>-th subroutine scope above the scope pointed by the invocant. This method can also be invoked as a class method, in which case it is equivalent to calling L on a L object for the current context. -If omitted, C<$frames> defaults to C<0>, which results in the closest sub enclosing the topic context. +If omitted, C<$frames> defaults to C<0>, which results in the closest sub enclosing the scope pointed by the invocant. outer(); @@ -278,15 +279,17 @@ If omitted, C<$frames> defaults to C<0>, which results in the closest sub enclos sub sub { my ($self, $frames) = @_; + my $cxt; if (Scalar::Util::blessed($self)) { $self->assert_valid; + $cxt = $self->cxt; } else { - $self = $self->new(Scope::Upper::UP(Scope::Upper::SUB())); + $cxt = Scope::Upper::UP(Scope::Upper::SUB()); } $frames = 0 unless defined $frames; - my $cxt = Scope::Upper::SUB($self->cxt); + $cxt = Scope::Upper::SUB($cxt); $cxt = Scope::Upper::SUB(Scope::Upper::UP($cxt)) for 1 .. $frames; $self->new($cxt); @@ -298,11 +301,11 @@ sub sub { my $eval_cxt = $cxt->eval($frames); my $eval_cxt = Scope::Context->eval; -Returns a new L object pointing to the C<$frames>-th C scope above the topic context. +Returns a new L object pointing to the C<$frames>-th C scope above the scope pointed by the invocant. This method can also be invoked as a class method, in which case it is equivalent to calling L on a L object for the current context. -If omitted, C<$frames> defaults to C<0>, which results in the closest eval enclosing the topic context. +If omitted, C<$frames> defaults to C<0>, which results in the closest eval enclosing the scope pointed by the invocant. eval { sub { @@ -316,15 +319,17 @@ If omitted, C<$frames> defaults to C<0>, which results in the closest eval enclo sub eval { my ($self, $frames) = @_; + my $cxt; if (Scalar::Util::blessed($self)) { $self->assert_valid; + $cxt = $self->cxt; } else { - $self = $self->new(Scope::Upper::UP(Scope::Upper::SUB())); + $cxt = Scope::Upper::UP(Scope::Upper::SUB()); } $frames = 0 unless defined $frames; - my $cxt = Scope::Upper::EVAL($self->cxt); + $cxt = Scope::Upper::EVAL($cxt); $cxt = Scope::Upper::EVAL(Scope::Upper::UP($cxt)) for 1 .. $frames; $self->new($cxt); @@ -334,7 +339,7 @@ sub eval { $cxt->reap($code); -Execute C<$code> when the topic context ends. +Execute C<$code> when the scope pointed by the invocant ends. See L for details. @@ -352,7 +357,7 @@ sub reap { $cxt->localize($what, $value); -Localize the variable described by C<$what> to the value C<$value> when the control flow returns to the scope pointed by the topic context. +Localize the variable described by C<$what> to the value C<$value> when the control flow returns to the scope pointed by the invocant. See L for details. @@ -370,7 +375,7 @@ sub localize { $cxt->localize_elem($what, $key, $value); -Localize the element C<$key> of the variable C<$what> to the value C<$value> when the control flow returns to the scope pointed by the topic context. +Localize the element C<$key> of the variable C<$what> to the value C<$value> when the control flow returns to the scope pointed by the invocant. See L for details. @@ -388,7 +393,7 @@ sub localize_elem { $cxt->localize_delete($what, $key); -Delete the element C<$key> from the variable C<$what> when the control flow returns to the scope pointed by the topic context. +Delete the element C<$key> from the variable C<$what> when the control flow returns to the scope pointed by the invocant. See L for details. @@ -406,7 +411,7 @@ sub localize_delete { $cxt->unwind(@values); -Immediately returns the scalars listed in C<@values> from the closest subroutine enclosing the topic context. +Immediately returns the scalars listed in C<@values> from the closest subroutine enclosing the scope pointed by the invocant. See L for details. @@ -420,11 +425,29 @@ sub unwind { Scope::Upper::unwind(@_ => $self->cxt); } +=head2 C + + $cxt->yield(@values); + +Immediately returns the scalars listed in C<@values> from the scope pointed by the invocant, whatever it may be (except a substitution eval context). + +See L for details. + +=cut + +sub yield { + my $self = shift; + + $self->assert_valid; + + Scope::Upper::yield(@_ => $self->cxt); +} + =head2 C my @ret = $cxt->uplevel($code, @args); -Executes the code reference C<$code> with arguments C<@args> in the same setting as the closest subroutine enclosing the topic context, then returns to the current scope the values returned by C<$code>. +Executes the code reference C<$code> with arguments C<@args> in the same setting as the closest subroutine enclosing the scope pointed by the invocant, then returns to the current scope the values returned by C<$code>. See L for details. @@ -443,7 +466,7 @@ sub uplevel { L (core module since perl 5), L (since 5.7.3). -L 0.18. +L 0.21. =head1 SEE ALSO @@ -470,7 +493,7 @@ You can find documentation for this module with the perldoc command. =head1 COPYRIGHT & LICENSE -Copyright 2011 Vincent Pit, all rights reserved. +Copyright 2011,2012,2013 Vincent Pit, all rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.