]> git.vpit.fr Git - perl/modules/Scope-Upper.git/blob - samples/try.pl
e207e14beef5e9b22bcd8213ed21e0e31fc6a79e
[perl/modules/Scope-Upper.git] / samples / try.pl
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use blib;
7
8 use Scope::Upper qw<unwind want_at :words>;
9
10 sub try (&) {
11  my @result = shift->();
12  my $cx = SUB UP; # Point to the sub above this one
13  unwind +(want_at($cx) ? @result : scalar @result) => $cx;
14 }
15
16 sub zap {
17  try {
18   my @things = qw<a b c>;
19   return @things; # returns to try() and then outside zap()
20  };
21  print "NOT REACHED\n";
22 }
23
24 my @stuff = zap(); # @stuff contains qw<a b c>
25 my $stuff = zap(); # $stuff contains 3
26
27 print "zap() returns @stuff in list context and $stuff in scalar context\n";