sub some_func { my $saveDir = new PreserveWD; # Do stuff } #### package PreserveWD; use strict 'vars'; use Carp; use Cwd; sub back { my $self = shift; chdir($$self) or confess("Cannot chdir to $$self: $!"); } sub new { my $cwd; return tie $cwd, 'PreserveWD'; } sub TIESCALAR { my ($type, $dir) = @_; $dir ||= cwd(); return bless \$dir, $type; } *DESTROY = *back; 1;