| 1 |
NAME |
| 2 |
File::pushd - change directory temporarily for a limited scope |
| 3 |
|
| 4 |
VERSION |
| 5 |
version 1.016 |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
use File::pushd; |
| 9 |
|
| 10 |
chdir $ENV{HOME}; |
| 11 |
|
| 12 |
# change directory again for a limited scope |
| 13 |
{ |
| 14 |
my $dir = pushd( '/tmp' ); |
| 15 |
# working directory changed to /tmp |
| 16 |
} |
| 17 |
# working directory has reverted to $ENV{HOME} |
| 18 |
|
| 19 |
# tempd() is equivalent to pushd( File::Temp::tempdir ) |
| 20 |
{ |
| 21 |
my $dir = tempd(); |
| 22 |
} |
| 23 |
|
| 24 |
# object stringifies naturally as an absolute path |
| 25 |
{ |
| 26 |
my $dir = pushd( '/tmp' ); |
| 27 |
my $filename = File::Spec->catfile( $dir, "somefile.txt" ); |
| 28 |
# gives /tmp/somefile.txt |
| 29 |
} |
| 30 |
|
| 31 |
DESCRIPTION |
| 32 |
File::pushd does a temporary "chdir" that is easily and automatically |
| 33 |
reverted, similar to "pushd" in some Unix command shells. It works by |
| 34 |
creating an object that caches the original working directory. When the |
| 35 |
object is destroyed, the destructor calls "chdir" to revert to the |
| 36 |
original working directory. By storing the object in a lexical variable |
| 37 |
with a limited scope, this happens automatically at the end of the |
| 38 |
scope. |
| 39 |
|
| 40 |
This is very handy when working with temporary directories for tasks |
| 41 |
like testing; a function is provided to streamline getting a temporary |
| 42 |
directory from File::Temp. |
| 43 |
|
| 44 |
For convenience, the object stringifies as the canonical form of the |
| 45 |
absolute pathname of the directory entered. |
| 46 |
|
| 47 |
Warning: if you create multiple "pushd" objects in the same lexical |
| 48 |
scope, their destruction order is not guaranteed and you might not wind |
| 49 |
up in the directory you expect. |
| 50 |
|
| 51 |
USAGE |
| 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{^("" in -+@\w.+)$}}, which is |
| 76 |
reasonably restrictive (e.g. it does not even allow spaces in the path). |
| 77 |
Change this to suit your circumstances and security needs if running |
| 78 |
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 |
"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 to |
| 107 |
be preserved. With an argument, the directory will be preserved if the |
| 108 |
argument is true, or marked for cleanup if the argument is false. Only |
| 109 |
"tempd" objects may be marked for cleanup. (Target directories to |
| 110 |
"pushd" are always preserved.) "preserve" returns true if the directory |
| 111 |
will be preserved, and false otherwise. |
| 112 |
|
| 113 |
DIAGNOSTICS |
| 114 |
"pushd" and "tempd" warn with message "Useless use of File::pushd::*%s* |
| 115 |
in void context" if called in void context and the warnings category |
| 116 |
"void" is enabled. |
| 117 |
|
| 118 |
{ |
| 119 |
use warnings 'void'; |
| 120 |
|
| 121 |
pushd(); |
| 122 |
} |
| 123 |
|
| 124 |
SEE ALSO |
| 125 |
* File::chdir |
| 126 |
|
| 127 |
SUPPORT |
| 128 |
Bugs / Feature Requests |
| 129 |
Please report any bugs or feature requests through the issue tracker at |
| 130 |
<https://github.com/dagolden/File-pushd/issues>. You will be notified |
| 131 |
automatically of any progress on your issue. |
| 132 |
|
| 133 |
Source Code |
| 134 |
This is open source software. The code repository is available for |
| 135 |
public review and contribution under the terms of the license. |
| 136 |
|
| 137 |
<https://github.com/dagolden/File-pushd> |
| 138 |
|
| 139 |
git clone https://github.com/dagolden/File-pushd.git |
| 140 |
|
| 141 |
AUTHOR |
| 142 |
David Golden <[email protected]> |
| 143 |
|
| 144 |
CONTRIBUTORS |
| 145 |
* Diab Jerius <[email protected]> |
| 146 |
|
| 147 |
* Graham Ollis <[email protected]> |
| 148 |
|
| 149 |
* Olivier Mengué <[email protected]> |
| 150 |
|
| 151 |
* Shoichi Kaji <[email protected]> |
| 152 |
|
| 153 |
COPYRIGHT AND LICENSE |
| 154 |
This software is Copyright (c) 2018 by David A Golden. |
| 155 |
|
| 156 |
This is free software, licensed under: |
| 157 |
|
| 158 |
The Apache License, Version 2.0, January 2004 |
| 159 |
|