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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
RE: Preserve working directory
by merlyn (Sage) on Sep 30, 2000 at 04:46 UTC | |
by Aristotle (Chancellor) on Dec 18, 2002 at 15:33 UTC |