1File::pushd(3) User Contributed Perl Documentation File::pushd(3)
2
3
4
6 File::pushd - change directory temporarily for a limited scope
7
9 version 1.005
10
12 use File::pushd;
13
14 chdir $ENV{HOME};
15
16 # change directory again for a limited scope
17 {
18 my $dir = pushd( '/tmp' );
19 # working directory changed to /tmp
20 }
21 # working directory has reverted to $ENV{HOME}
22
23 # tempd() is equivalent to pushd( File::Temp::tempdir )
24 {
25 my $dir = tempd();
26 }
27
28 # object stringifies naturally as an absolute path
29 {
30 my $dir = pushd( '/tmp' );
31 my $filename = File::Spec->catfile( $dir, "somefile.txt" );
32 # gives /tmp/somefile.txt
33 }
34
36 File::pushd does a temporary "chdir" that is easily and automatically
37 reverted, similar to "pushd" in some Unix command shells. It works by
38 creating an object that caches the original working directory. When
39 the object is destroyed, the destructor calls "chdir" to revert to the
40 original working directory. By storing the object in a lexical
41 variable with a limited scope, this happens automatically at the end of
42 the scope.
43
44 This is very handy when working with temporary directories for tasks
45 like testing; a function is provided to streamline getting a temporary
46 directory from File::Temp.
47
48 For convenience, the object stringifies as the canonical form of the
49 absolute pathname of the directory entered.
50
52 use File::pushd;
53
54 Using File::pushd automatically imports the "pushd" and "tempd"
55 functions.
56
57 pushd
58 {
59 my $dir = pushd( $target_directory );
60 }
61
62 Caches the current working directory, calls "chdir" to change to the
63 target directory, and returns a File::pushd object. When the object is
64 destroyed, the working directory reverts to the original directory.
65
66 The provided target directory can be a relative or absolute path. If
67 called with no arguments, it uses the current directory as its target
68 and returns to the current directory when the object is destroyed.
69
70 If the target directory does not exist or if the directory change fails
71 for some reason, "pushd" will die with an error message.
72
73 Can be given a hashref as an optional second argument. The only
74 supported option is "untaint_pattern", which is used to untaint file
75 paths involved. It defaults to "qr{^([-+@\w./]+)$}", which is
76 reasonably restrictive (e.g. it does not even allow spaces in the
77 path). Change this to suit your circumstances and security needs if
78 running under taint mode. Note: you must include the parentheses in the
79 pattern to capture the untainted portion of the path.
80
81 tempd
82 {
83 my $dir = tempd();
84 }
85
86 This function is like "pushd" but automatically creates and calls
87 "chdir" to a temporary directory created by File::Temp. Unlike normal
88 File::Temp cleanup which happens at the end of the program, this
89 temporary directory is removed when the object is destroyed. (But also
90 see "preserve".) A warning will be issued if the directory cannot be
91 removed.
92
93 As with "pushd", "tempd" will die if "chdir" fails.
94
95 It may be given a single options hash that will be passed internally to
96 C<pushd>.
97
98 preserve
99 {
100 my $dir = tempd();
101 $dir->preserve; # mark to preserve at end of scope
102 $dir->preserve(0); # mark to delete at end of scope
103 }
104
105 Controls whether a temporary directory will be cleaned up when the
106 object is destroyed. With no arguments, "preserve" sets the directory
107 to be preserved. With an argument, the directory will be preserved if
108 the argument is true, or marked for cleanup if the argument is false.
109 Only "tempd" objects may be marked for cleanup. (Target directories to
110 "pushd" are always preserved.) "preserve" returns true if the
111 directory will be preserved, and false otherwise.
112
114 ยท File::chdir
115
117 Bugs / Feature Requests
118 Please report any bugs or feature requests through the issue tracker at
119 <https://github.com/dagolden/file-pushd/issues>. You will be notified
120 automatically of any progress on your issue.
121
122 Source Code
123 This is open source software. The code repository is available for
124 public review and contribution under the terms of the license.
125
126 <https://github.com/dagolden/file-pushd>
127
128 git clone git://github.com/dagolden/file-pushd.git
129
131 David Golden <dagolden@cpan.org>
132
134 Diab Jerius <djerius@cfa.harvard.edu>
135
137 This software is Copyright (c) 2013 by David A Golden.
138
139 This is free software, licensed under:
140
141 The Apache License, Version 2.0, January 2004
142
143
144
145perl v5.16.3 2013-03-22 File::pushd(3)