6 use vars qw($VERSION @ISA @EXPORT $CWD @CWD);
11 @EXPORT = qw($CWD @CWD);
16 tie $CWD, 'File::chdir::SCALAR' or die "Can't tie \$CWD";
17 tie @CWD, 'File::chdir::ARRAY' or die "Can't tie \@CWD";
22 File::chdir - a more sensible way to change directories
28 $CWD = "/foo/bar"; # now in /foo/bar
30 local $CWD = "/moo/baz"; # now in /moo/baz
38 Perl's chdir() has the unfortunate problem of being very, very, very
39 global. If any part of your program calls chdir() or if any library
40 you use calls chdir(), it changes the current working directory for
45 File::chdir gives you an alternative, $CWD and @CWD. These two
46 variables combine all the power of C<chdir()>, File::Spec and Cwd.
50 Use the $CWD variable instead of chdir() and Cwd.
53 $CWD = $dir; # just like chdir($dir)!
54 print $CWD; # prints the current working directory
56 It can be localized, and it does the right thing.
58 $CWD = "/foo"; # it's /foo out here.
60 local $CWD = "/bar"; # /bar in here
62 # still /foo out here!
64 $CWD always returns the absolute path.
66 $CWD and normal chdir() work together just fine.
70 @CWD represents the current working directory as an array, each
71 directory in the path is an element of the array. This can often make
72 the directory easier to manipulate, and you don't have to fumble with
73 C<File::Spec-E<gt>splitpath> and C<File::Spec-E<gt>catdir> to make
76 # Similar to chdir("/usr/local/src/perl")
77 @CWD = qw(usr local src perl);
79 pop, push, shift, unshift and splice all work. pop and push are
80 probably the most useful.
82 pop @CWD; # same as chdir(File::Spec->updir)
83 push @CWD, 'some_dir' # same as chdir('some_dir')
85 @CWD and $CWD both work fine together.
87 B<NOTE> Due to a perl bug you can't localize @CWD. See L</BUGS and
88 CAVEATS> for a work around.
93 # Otherwise we'll never work under taint mode.
94 my($cwd) = Cwd::abs_path =~ /(.*)/;
102 my $Real_CWD = File::Spec->catdir(_abs_path(), $new_dir);
104 return CORE::chdir($new_dir);
108 package File::chdir::SCALAR;
114 # To be safe, in case someone chdir'd out from under us, we always
115 # check the Cwd explicitly.
117 return File::chdir::_abs_path;
121 return unless defined $_[1];
122 my $did_chdir = File::chdir::_chdir($_[1]);
123 return $did_chdir ? $Real_CWD : $did_chdir;
129 package File::chdir::ARRAY;
135 # splitdir() leaves empty directory names in place on purpose.
136 # I don't think this is the right thing for us, but I could be wrong.
138 return grep length, File::Spec->splitdir($_[0]);
142 return _splitdir(File::chdir::_abs_path);
146 return File::Spec->catdir(File::Spec->rootdir, @_);
150 my($self, $idx) = @_;
156 my($self, $idx, $val) = @_;
159 if( $self->{Cleared} ) {
160 $self->{Cleared} = 0;
167 my $dir = _catdir(@cwd);
169 my $did_chdir = File::chdir::_chdir($dir);
170 return $did_chdir ? $dir : $did_chdir;
173 sub FETCHSIZE { return scalar _cwd_list(); }
179 my $dir = _catdir(_cwd_list, @_);
180 my $did_chdir = File::chdir::_chdir($dir);
181 return $did_chdir ? $self->FETCHSIZE : $did_chdir;
188 my $popped = pop @cwd;
189 my $dir = _catdir(@cwd);
190 my $did_chdir = File::chdir::_chdir($dir);
191 return $did_chdir ? $popped : $did_chdir;
198 my $shifted = shift @cwd;
199 my $dir = _catdir(@cwd);
200 my $did_chdir = File::chdir::_chdir($dir);
201 return $did_chdir ? $shifted : $did_chdir;
207 my $dir = _catdir(@_, _cwd_list);
208 my $did_chdir = File::chdir::_chdir($dir);
209 return $did_chdir ? $self->FETCHSIZE : $did_chdir;
214 $self->{Cleared} = 1;
219 my $offset = shift || 0;
220 my $len = shift || $self->FETCHSIZE - $offset;
224 my @orig_dirs = splice @cwd, $offset, $len, @new_dirs;
225 my $dir = _catdir(@cwd);
226 my $did_chdir = File::chdir::_chdir($dir);
227 return $did_chdir ? @orig_dirs : $did_chdir;
232 my($self, $idx) = @_;
233 return $self->FETCHSIZE >= $idx ? 1 : 0;
237 die "Even I can't think of what delete \$CWD[\$idx] should do!";
244 (We omit the C<use File::chdir> from these examples for terseness)
246 Here's $CWD instead of chdir:
248 $CWD = 'foo'; # chdir('foo')
250 and now instead of Cwd.
252 print $CWD; # use Cwd; print Cwd::abs_path
254 you can even do zsh style C<cd foo bar>
256 $CWD = '/usr/local/foo';
259 if you want to localize that, make sure you get the parens right
262 (local $CWD) =~ s/usr/var/;
266 It's most useful for writing polite subroutines which don't leave the
267 program in some strange directory:
270 local $CWD = 'some/other/dir';
274 which is much simplier than the equivalent:
278 my $orig_dir = Cwd::abs_path;
279 chdir('some/other/dir');
286 @CWD comes in handy when you want to start moving up and down the
287 directory hierarchy in a cross-platform manner without having to use
290 pop @CWD; # chdir(File::Spec->updir);
291 push @CWD, 'some', 'dir' # chdir(File::Spec->catdir(qw(some dir)));
293 You can easily change your parent directory:
295 # chdir from /some/dir/bar/moo to /some/dir/foo/moo
299 =head1 BUGS and CAVEATS
301 C<local @CWD> will not localize C<@CWD>. This is a bug in Perl, you
302 can't localize tied arrays. As a work around localizing $CWD will
303 effectively localize @CWD.
314 What should %CWD do? Something with volumes?
316 # chdir to C:\Program Files\Sierra\Half Life ?
317 $CWD{C} = '\\Program Files\\Sierra\\Half Life';
322 Michael G Schwern E<lt>schwern@pobox.comE<gt>
327 Copyright 2001-2003 by Michael G Schwern E<lt>schwern@pobox.comE<gt>.
329 This program is free software; you can redistribute it and/or
330 modify it under the same terms as Perl itself.
332 See F<http://www.perl.com/perl/misc/Artistic.html>
337 I wanted C<local chdir> to work. p5p didn't. Did I let that stop me?
338 No! Did we give up after the Germans bombed Pearl Harbor? Hell, no!
340 Abigail and/or Bryan Warnock suggested the $CWD thing, I forget which.
343 The chdir() override was eliminated in 0.04.
348 File::Spec, Cwd, L<perlfunc/chdir>