]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blobdiff - samples/try.pl
This is 0.34
[perl/modules/Scope-Upper.git] / samples / try.pl
index 305a0ec3bef65ec86220ecd60386bd0adf11d595..38b0e50152a312883c9f86f7901347e5d227bbb1 100644 (file)
@@ -5,23 +5,42 @@ use warnings;
 
 use blib;
 
-use Scope::Upper qw/unwind want_at :words/;
+use Scope::Upper qw<unwind want_at :words>;
 
 sub try (&) {
  my @result = shift->();
- my $cx = SUB UP SUB;
+ 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/a b c/;
+  my @things = qw<a b c>;
   return @things; # returns to try() and then outside zap()
  };
  print "NOT REACHED\n";
 }
 
-my @what = zap(); # @what contains qw/a b c/
-my $what = zap(); # $what contains 3
+my @stuff = zap(); # @stuff contains qw<a b c>
+my $stuff = zap(); # $stuff contains 3
 
-print "zap() returns @what in list context and $what in scalar context\n";
+print "zap() returns @stuff in list context and $stuff in scalar context\n";
+
+{
+ package Uplevel;
+
+ use Scope::Upper qw<uplevel CALLER>;
+
+ sub target {
+  faker(@_);
+ }
+
+ sub faker {
+  uplevel {
+   my $sub = (caller 0)[3];
+   print "$_[0] from $sub()\n";
+  } @_ => CALLER(1);
+ }
+
+ target('hello'); # "hello from Uplevel::target()"
+}