]> git.vpit.fr Git - perl/modules/Scope-Context.git/blobdiff - lib/Scope/Context.pm
Implement ->yield
[perl/modules/Scope-Context.git] / lib / Scope / Context.pm
index 4a45cbb540c61f2b7b60a2d0e2e0d587b07be42d..85fd57365e7eeacf839251b86a28d9f7075673ca 100644 (file)
@@ -8,7 +8,7 @@ use warnings;
 use Carp         ();
 use Scalar::Util ();
 
-use Scope::Upper 0.18 ();
+use Scope::Upper 0.21 ();
 
 =head1 NAME
 
@@ -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);
@@ -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);
@@ -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);
@@ -420,6 +425,24 @@ sub unwind {
  Scope::Upper::unwind(@_ => $self->cxt);
 }
 
+=head2 C<yield>
+
+    $cxt->yield(@values);
+
+Immediately returns the scalars listed in C<@values> from the topic context, whatever it may be (except a substitution eval context).
+
+See L<Scope::Upper/yield> for details.
+
+=cut
+
+sub yield {
+ my $self = shift;
+
+ $self->assert_valid;
+
+ Scope::Upper::yield(@_ => $self->cxt);
+}
+
 =head2 C<uplevel>
 
     my @ret = $cxt->uplevel($code, @args);
@@ -443,7 +466,7 @@ sub uplevel {
 
 L<Carp> (core module since perl 5), L<Scalar::Util> (since 5.7.3).
 
-L<Scope::Upper> 0.18.
+L<Scope::Upper> 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 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.