Created
June 3, 2010 07:00
-
-
Save jrockway/423578 to your computer and use it in GitHub Desktop.
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
| #!/usr/bin/env perl | |
| use strict; | |
| use warnings; | |
| use feature ':5.10'; | |
| use Guard; | |
| sub unwind_protect(&@){ | |
| my $code = shift; | |
| my %args = @_; | |
| my $after = $args{cleanup}; | |
| scope_guard { $after->() }; | |
| return $code->(); | |
| } | |
| use Cwd; | |
| my $start = getcwd; | |
| say "start: $start"; | |
| eval { | |
| unwind_protect { | |
| chdir '/'; | |
| say "now: ", getcwd; | |
| die "OH NOES!!!!"; | |
| } cleanup => sub { chdir $start }; | |
| }; | |
| say "but back to: ", getcwd; | |
| OHNOES: for (1) { | |
| unwind_protect { | |
| chdir '/'; | |
| say "now: ", getcwd; | |
| no warnings 'exiting'; | |
| last OHNOES; | |
| } cleanup => sub { chdir $start }; | |
| }; | |
| say "and finally: ", getcwd; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment