On Sat Dec 08 23:57:58 2007, KWILLIAMS wrote:
Show quoted text> However, I would love to add a *scoped* chdir(), so you could chdir()
> into a directory and then automatically chdir() back out of it, even
> if an exception is thrown.
Currently, it can be done using
use Sub::ScopeFinalizer qw( scope_finalizer );
{
my $orig_dir = Path::Class::Dir->new('.')->absolute();
chdir($some_dir)
or die("Can't change directory: $!\n");
my $sentry = scope_finalizer {
chdir($orig_dir)
or die("Can't restore directory: $!\n");
};
...
die if $cond;
...
}
So you could do
use Sub::ScopeFinalizer qw( scope_finalizer );
sub scoped_chdir {
my $self = shift;
my $orig_dir = Path::Class::Dir->new('.')->absolute();
chdir($self)
or return;
return scope_finalizer {
chdir($orig_dir)
or die("Can't restore directory: $!\n");
};
}
Which would be called as follows:
{
my $sentry = $some_dir->scoped_chdir()
or die("Can't change to dir $some_dir: $!\n");
...
die if $cond;
...
}
Is it worth it? dunno.