Next | Tricks of the Wizards | 132 |
Here's the idea:
{ my $temporary = LocalChdir->chdir_to($DIR); ... }
When control exits the block, $temporary will be destroyed
We can rig up LocalChdir::DESTROY to move back to the old directory
package LocalChdir; use Cwd;
sub chdir_to { my ($package, $new_dir) = @_; my $old_dir = cwd(); return unless chdir($new_dir); bless { DIR => $old_dir } => $package; }
sub DESTROY { my $dir = $_[0]{DIR}; chdir($dir) or croak("Couldn't return to '$dir' on block exit: $!"); }
Next | Copyright © 2003 M. J. Dominus |