From: Vincent Pit Date: Tue, 17 Mar 2015 13:40:56 +0000 (-0300) Subject: Update synopsis X-Git-Tag: v0.03~10 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FScope-Context.git;a=commitdiff_plain;h=78cfdeb253000a116c0c48488d2d14dbf61e6d06 Update synopsis --- diff --git a/lib/Scope/Context.pm b/lib/Scope/Context.pm index 2789f5d..ffc139e 100644 --- a/lib/Scope/Context.pm +++ b/lib/Scope/Context.pm @@ -29,32 +29,33 @@ our $VERSION = '0.02'; for (1 .. 5) { sub { eval { - # Create Scope::Context objects for different upper frames. - my ($block, $sub, $eval, $loop); + # Create Scope::Context objects for different upper frames : + my ($block, $eval, $sub, $loop); { $block = Scope::Context->new; - $sub = $block->sub; # = $block->up - $eval = $block->eval; # = $block->up(2) - $loop = $eval->up; # = $block->up(3) + $eval = $block->eval; # == $block->up + $sub = $block->sub; # == $block->up(2) + $loop = $sub->up; # == $block->up(3) } eval { - # This will throw an exception, since $block has expired. + # This throws an exception, since $block has expired : $block->localize('$x' => 1); }; - # This prints "hello" when the eval block above ends. + # This will print "hello" when the current eval block ends : $eval->reap(sub { print "hello\n" }); - # Ignore $SIG{__DIE__} just for the loop body. - $loop->localize_delete('%SIG', '__DIE__'); + # Ignore warnings just for the loop body : + $loop->localize_elem('%SIG', __WARN__ => sub { }); - # Execute the callback as if it ran in place of the sub. + # Execute the callback as if it ran in place of the sub : my @values = $sub->uplevel(sub { return @_, 2; }, 1); + # @values now contains (1, 2). - # Immediately return (1, 2, 3) from the sub, bypassing the eval. + # Immediately return (1, 2, 3) from the sub, bypassing the eval : $sub->unwind(@values, 3); # Not reached. @@ -63,10 +64,13 @@ our $VERSION = '0.02'; # Not reached. }->(); - # unwind() returns here. "hello\n" was printed, and now - # $SIG{__DIE__} is undefined. + # unwind() returns here. "hello\n" was printed, and now warnings are + # ignored. } + # $SIG{__WARN__} has been restored to its original value, warnings are no + # longer ignored. + =head1 DESCRIPTION This class provides an object-oriented interface to L's functionalities. diff --git a/samples/synopsis.pl b/samples/synopsis.pl index c531098..556dd01 100644 --- a/samples/synopsis.pl +++ b/samples/synopsis.pl @@ -7,38 +7,43 @@ use blib; use Scope::Context; +local $" = ', '; + for my $run (1 .. 2) { my @values = sub { local $@; eval { - # create Scope::Context objects - my ($block, $sub, $eval, $loop); + # Create Scope::Context objects for different upper frames : + my ($block, $eval, $sub, $loop); { $block = Scope::Context->new; - $sub = $block->sub; # = $block->up - $eval = $block->eval; # = $block->up(2) - $loop = $eval->up; # = $block->up(3) + $eval = $block->eval; # == $block->up + $sub = $block->sub; # == $block->up(2) + $loop = $sub->up; # == $block->up(3) } eval { - # This will throw an exception, since $block has expired. + # This throws an exception, since $block has expired : $block->localize('$x' => 1); }; print "Caught an error at run $run: $@" if $@; - # This prints "hello" when the eval block above ends. + # This will print "End of eval scope..." when the current eval block ends : $eval->reap(sub { print "End of eval scope at run $run\n" }); - # Ignore $SIG{__DIE__} just for the loop. - $loop->localize_delete('%SIG', '__DIE__'); + # Ignore warnings just for the loop body : + $loop->localize_elem('%SIG', __WARN__ => sub { }); + # But for now they are still processed : + warn "This is a warning at run $run\n"; - # Execute the callback as if it ran in place of the sub. + # Execute the callback as if it ran in place of the sub : my @values = $sub->uplevel(sub { return @_, 2; }, 1); + print "After uplevel, \@values contains (@values) at run $run\n"; - # Immediately return (1, 2, 3) from the sub, bypassing the eval. + # Immediately return (1, 2, 3) from the sub, bypassing the eval : $sub->unwind(@values, 3); # Not reached. @@ -49,5 +54,10 @@ for my $run (1 .. 2) { die $@ if $@; }->(); - print "Values returned at run $run: @values\n"; + print "Values returned at run $run: (@values)\n"; + + # warnings are ignored, so this will be completely silent. + warn "You will not see this at run $run\n"; } + +warn "Warnings have been restored\n";