Scope::Upper

Act on upper scopes
Download

Scope::Upper Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Vincent Pit
  • Publisher web site:
  • http://search.cpan.org/~vpit/

Scope::Upper Tags


Scope::Upper Description

Act on upper scopes SYNOPSIS package X; use Scope::Upper qw/reap localize localize_elem localize_delete :words/; sub desc { shift->{desc} } sub set_tag { my ($desc) = @_; # First localize $x so that it gets destroyed last localize '$x' => bless({ desc => $desc }, __PACKAGE__) => UP; # one scope up reap sub { my $pkg = caller; my $x = do { no strict 'refs'; ${$pkg.'::x'} }; # Get the $x in the scope print $x->desc . ": done "; } => SCOPE 1; # same as UP here localize_elem '%SIG', '__WARN__' => sub { my $pkg = caller; my $x = do { no strict 'refs'; ${$pkg.'::x'} }; # Get the $x in the scope CORE::warn($x->desc . ': ' . join('', @_)); } => UP CALLER 0; # same as UP here # delete last @ARGV element localize_delete '@ARGV', -1 => UP SUB HERE; # same as UP here } package Y; { X::set_tag('pie'); # $x is now a X object, and @ARGV has one element less warn 'what'; # warns "pie: what at ..." ... } # "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() # not reached } # not reached } my @what = zap(); # @what contains @thingsScope::Upper is a Perl module that lets you defer actions at run-time that will take place when the control flow returns into an upper scope. Currently, you can: * hook an upper scope end with "reap" ; * localize variables, array/hash values or deletions of elements in higher contexts with respectively "localize", "localize_elem" and "localize_delete" ; * return values immediately to an upper level with "unwind", and know which context was in use then with "want_at". Requirements: · Perl


Scope::Upper Related Software