Skip to content

Instantly share code, notes, and snippets.

@MidLifeXis
Created October 3, 2012 12:27
Show Gist options
  • Select an option

  • Save MidLifeXis/3826679 to your computer and use it in GitHub Desktop.

Select an option

Save MidLifeXis/3826679 to your computer and use it in GitHub Desktop.
Scopeguard example
# From Try::Tiny
{
package # hide from PAUSE
Try::Tiny::ScopeGuard;
sub _new {
shift;
bless [ @_ ];
}
sub DESTROY {
my @guts = @{ shift() };
my $code = shift @guts;
$code->(@guts);
}
}
# Used
# set up a scope guard to invoke the finally block at the end
my @guards =
map { Try::Tiny::ScopeGuard->_new($_, $failed ? $error : ()) }
@finally;
@guards falls out of scope, DESTROY calls schtuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment