Created
October 3, 2012 12:27
-
-
Save MidLifeXis/3826679 to your computer and use it in GitHub Desktop.
Scopeguard example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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