From: Vincent Pit Date: Sun, 11 Jan 2009 17:54:42 +0000 (+0100) Subject: Show unwind() and want_at() in the synopsys and the samples X-Git-Tag: v0.04~3 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FScope-Upper.git;a=commitdiff_plain;h=086bb57f3e5a02fc333105c08b3ce99318f94fe6 Show unwind() and want_at() in the synopsys and the samples --- diff --git a/MANIFEST b/MANIFEST index 4041009..6ddf912 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ README Upper.xs lib/Scope/Upper.pm samples/tag.pl +samples/try.pl t/00-load.t t/01-import.t t/05-words.t diff --git a/lib/Scope/Upper.pm b/lib/Scope/Upper.pm index a315552..ba26e61 100644 --- a/lib/Scope/Upper.pm +++ b/lib/Scope/Upper.pm @@ -56,6 +56,26 @@ BEGIN { ... } # "pie: done" is printed + package Z; + + use Scope::Upper qw/unwind want_at :words/; + + sub try (&) { + my @result = shift->(); + my $cx = SUB UP SUB; + unwind +(want_at($cx) ? @result : scalar @result) => $cx; + } + + ... + + sub zap { + try { + return @things; # returns to try() and then outside zap() + } + } + + my @what = zap(); # @what contains @things + =head1 DESCRIPTION This module lets you defer actions that will take place when the control flow returns into an upper scope. diff --git a/samples/try.pl b/samples/try.pl new file mode 100644 index 0000000..d49c183 --- /dev/null +++ b/samples/try.pl @@ -0,0 +1,27 @@ +#!perl + +use strict; +use warnings; + +use blib; + +use Scope::Upper qw/unwind want_at :words/; + +sub try (&) { + my @result = shift->(); + my $cx = SUB UP SUB; + unwind +(want_at($cx) ? @result : scalar @result) => $cx; +} + +sub zap { + try { + my @things = qw/a b c/; + return @things; # returns to try() and then outside zap() + }; + print "NOT REACHED\n"; +} + +my @what = zap(); # @what contains @things +my $what = zap(); # @what contains @things + +print "zap() returns @what in list context and $what in scalar context\n";