ReleaseAction

ReleaseAction - call actions upon release.
Download

ReleaseAction Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Ben Tilly
  • Publisher web site:
  • http://search.cpan.org/~tilly/Text-xSV-0.16/lib/Text/xSV.pm

ReleaseAction Tags


ReleaseAction Description

ReleaseAction - call actions upon release. ReleaseAction - call actions upon release.SYNOPSIS use ReleaseAction 'on_release'; { # OO style my $handle = ReleaseAction->new( sub {print "Exiting scopen"} ); print "In scopen"; } { # Functional style my $handle = on_release {print "Exiting scopen"}; print "In scopen"; } { my $rollback = on_release {rollback_trans()}; if (do_stuff()) { $rollback->cancel(); }}This provides an easy way to create opaque handles which will do something when they are destroyed. There are two ways of creating a new handle. Both take one or more arguments, with the first being the action to take when the handle is released and the (optional) rest being the arguments that the handle will get.new is the method oriented constructor. my $handle = ReleaseAction->new( sub {print shift}, "Goodbye cruel worldn" );And an optional function on_release that you can import. For those who like that sort of thing, I have provided the prototype &@ for syntactic sugar. my $handle = on_release {print "Goodbye cruel worldn"};And should you decide that you don't want to do the action on release after all, you can call the cancel() method. As suggested in the SYNOPSIS, this is useful if you wish to set up transactional mechanics. Make the release action do your cleanup. And then when you commit your changes, cancel the cleanup.EXAMPLE use ReleaseAction 'on_release'; # This does the same thing as the module SelectSaver. sub tmp_select { on_release {select shift} select shift; } print "This print goes to STDOUTn"; { my $hold_select = tmp_select(*STDERR); print "This print goes to STDERRn"; } print "Printing to STDOUT againn";A LONGER EXAMPLE use Carp; use Cwd; use ReleaseAction; sub cd_to { chdir($_) or confess("Cannot chdir to $_: $!"); } sub tmp_cd { my $cwd = cwd(); cd_to(shift); ReleaseAction->new(&cd_to, $cwd); } sub something_interesting { my $in_dir = tmp_cd("some_dir"); # Do something interesting in the new dir # I will automagically return to the old dir # when I exit the subroutine and $in_dir goes # out of scope. } Requirements: · Perl


ReleaseAction Related Software