Next Tricks of the Wizards 132

Local Effects

        { my $temporary = LocalChdir->chdir_to($DIR);
          ...
        }
        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