Debian Codegraph @

README

raw
1 NAME
2 File::chdir - a more sensible way to change directories
3
4 VERSION
5 version 0.1011
6
7 SYNOPSIS
8 use File::chdir;
9
10 $CWD = "/foo/bar"; # now in /foo/bar
11 {
12 local $CWD = "/moo/baz"; # now in /moo/baz
13 ...
14 }
15
16 # still in /foo/bar!
17
18 DESCRIPTION
19 Perl's "chdir()" has the unfortunate problem of being very, very, very
20 global. If any part of your program calls "chdir()" or if any library
21 you use calls "chdir()", it changes the current working directory for
22 the *whole* program.
23
24 This sucks.
25
26 File::chdir gives you an alternative, $CWD and @CWD. These two variables
27 combine all the power of "chdir()", File::Spec and Cwd.
28
29 $CWD
30 Use the $CWD variable instead of "chdir()" and Cwd.
31
32 use File::chdir;
33 $CWD = $dir; # just like chdir($dir)!
34 print $CWD; # prints the current working directory
35
36 It can be localized, and it does the right thing.
37
38 $CWD = "/foo"; # it's /foo out here.
39 {
40 local $CWD = "/bar"; # /bar in here
41 }
42 # still /foo out here!
43
44 $CWD always returns the absolute path in the native form for the
45 operating system.
46
47 $CWD and normal "chdir()" work together just fine.
48
49 @CWD
50 @CWD represents the current working directory as an array, each
51 directory in the path is an element of the array. This can often make
52 the directory easier to manipulate, and you don't have to fumble with
53 "File::Spec->splitpath" and "File::Spec->catdir" to make portable code.
54
55 # Similar to chdir("/usr/local/src/perl")
56 @CWD = qw(usr local src perl);
57
58 pop, push, shift, unshift and splice all work. pop and push are probably
59 the most useful.
60
61 pop @CWD; # same as chdir(File::Spec->updir)
62 push @CWD, 'some_dir' # same as chdir('some_dir')
63
64 @CWD and $CWD both work fine together.
65
66 *NOTE* Due to a perl bug you can't localize @CWD. See "CAVEATS" for a
67 work around.
68
69 EXAMPLES
70 (We omit the "use File::chdir" from these examples for terseness)
71
72 Here's $CWD instead of "chdir()":
73
74 $CWD = 'foo'; # chdir('foo')
75
76 and now instead of Cwd.
77
78 print $CWD; # use Cwd; print Cwd::abs_path
79
80 you can even do zsh style "cd foo bar"
81
82 $CWD = '/usr/local/foo';
83 $CWD =~ s/usr/var/;
84
85 if you want to localize that, make sure you get the parens right
86
87 {
88 (local $CWD) =~ s/usr/var/;
89 ...
90 }
91
92 It's most useful for writing polite subroutines which don't leave the
93 program in some strange directory:
94
95 sub foo {
96 local $CWD = 'some/other/dir';
97 ...do your work...
98 }
99
100 which is much simpler than the equivalent:
101
102 sub foo {
103 use Cwd;
104 my $orig_dir = Cwd::getcwd;
105 chdir('some/other/dir');
106
107 ...do your work...
108
109 chdir($orig_dir);
110 }
111
112 @CWD comes in handy when you want to start moving up and down the
113 directory hierarchy in a cross-platform manner without having to use
114 File::Spec.
115
116 pop @CWD; # chdir(File::Spec->updir);
117 push @CWD, 'some', 'dir' # chdir(File::Spec->catdir(qw(some dir)));
118
119 You can easily change your parent directory:
120
121 # chdir from /some/dir/bar/moo to /some/dir/foo/moo
122 $CWD[-2] = 'foo';
123
124 CAVEATS
125 "local @CWD" does not work.
126 "local @CWD" will not localize @CWD. This is a bug in Perl, you can't
127 localize tied arrays. As a work around localizing $CWD will effectively
128 localize @CWD.
129
130 {
131 local $CWD;
132 pop @CWD;
133 ...
134 }
135
136 Assigning to @CWD calls "chdir()" for each element
137 @CWD = qw/a b c d/;
138
139 Internally, Perl clears @CWD and assigns each element in turn. Thus,
140 this code above will do this:
141
142 chdir 'a';
143 chdir 'a/b';
144 chdir 'a/b/c';
145 chdir 'a/b/c/d';
146
147 Generally, avoid assigning to @CWD and just use push and pop instead.
148
149 Volumes not handled
150 There is currently no way to change the current volume via File::chdir.
151
152 NOTES
153 $CWD returns the current directory using native path separators, i.e.
154 "\" on Win32. This ensures that $CWD will compare correctly with
155 directories created using File::Spec. For example:
156
157 my $working_dir = File::Spec->catdir( $CWD, "foo" );
158 $CWD = $working_dir;
159 doing_stuff_might_chdir();
160 is( $CWD, $working_dir, "back to original working_dir?" );
161
162 Deleting the last item of @CWD will act like a pop. Deleting from the
163 middle will throw an exception.
164
165 delete @CWD[-1]; # OK
166 delete @CWD[-2]; # Dies
167
168 What should %CWD do? Something with volumes?
169
170 # chdir to C:\Program Files\Sierra\Half Life ?
171 $CWD{C} = '\\Program Files\\Sierra\\Half Life';
172
173 DIAGNOSTICS
174 If an error is encountered when changing $CWD or @CWD, one of the
175 following exceptions will be thrown:
176
177 * ~Can't delete except at the end of @CWD~ * ~Failed to change directory
178 to '$dir'~
179
180 HISTORY
181 Michael wanted "local chdir" to work. p5p didn't. But it wasn't over!
182 Was it over when the Germans bombed Pearl Harbor? Hell, no!
183
184 Abigail and/or Bryan Warnock suggested the $CWD thing (Michael forgets
185 which). They were right.
186
187 The "chdir()" override was eliminated in 0.04.
188
189 David became co-maintainer with 0.06_01 to fix some chronic Win32 path
190 bugs.
191
192 As of 0.08, if changing $CWD or @CWD fails to change the directory, an
193 error will be thrown.
194
195 SEE ALSO
196 File::pushd, File::Spec, Cwd, "chdir" in perlfunc, "Animal House"
197 <http://www.imdb.com/title/tt0077975/quotes>
198
199 SUPPORT
200 Bugs / Feature Requests
201 Please report any bugs or feature requests through the issue tracker at
202 <https://github.com/dagolden/File-chdir/issues>. You will be notified
203 automatically of any progress on your issue.
204
205 Source Code
206 This is open source software. The code repository is available for
207 public review and contribution under the terms of the license.
208
209 <https://github.com/dagolden/File-chdir>
210
211 git clone https://github.com/dagolden/File-chdir.git
212
213 AUTHORS
214 * David Golden <[email protected]>
215
216 * Michael G. Schwern <[email protected]>
217
218 CONTRIBUTORS
219 * David Golden <[email protected]>
220
221 * Joel Berger <[email protected]>
222
223 * Philippe Bruhat (BooK) <[email protected]>
224
225 COPYRIGHT AND LICENSE
226 This software is copyright (c) 2016 by Michael G. Schwern and David
227 Golden.
228
229 This is free software; you can redistribute it and/or modify it under
230 the same terms as the Perl 5 programming language system itself.
231