in reply to Re: Testing the current directory with Cwd and File::Spec
in thread Testing the current directory with Cwd and File::Spec
I disagree. The easiest way to save the current directory and return to it later is File::chdir, which lets you change directories through a localized variable. Example:
use File::chdir; print $CWD; # now cd and show me a directory listing { local(@CWD,$CWD); push @CWD,'subdir'; print $CWD; print join("\n", glob '*'); } # now we're back where we started. We'll do a listing to show. print $CWD; print join("\n", glob '*');
The imported variable $CWD always holds the full path, so you can also just store it to a variable and restore the value later, even mixing things with chdir.
use File::chdir; my $wd = $CWD; chdir('subdir'); # do some work. $CWD = $wd; # return to saved dir.
Now, that may not be the best way, but I'd vote for it as easiest. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Testing the current directory with Cwd and File::Spec
by xdg (Monsignor) on Dec 06, 2005 at 00:32 UTC | |
by Celada (Monk) on Dec 06, 2005 at 05:45 UTC |