Skip to content

Instantly share code, notes, and snippets.

@jrockway
Created June 3, 2010 07:00
Show Gist options
  • Select an option

  • Save jrockway/423578 to your computer and use it in GitHub Desktop.

Select an option

Save jrockway/423578 to your computer and use it in GitHub Desktop.
#!/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