1Cwd(3) User Contributed Perl Documentation Cwd(3)
2
3
4
6 Cwd - get pathname of current working directory
7
9 use Cwd;
10 my $dir = getcwd;
11
12 use Cwd 'abs_path';
13 my $abs_path = abs_path($file);
14
16 This module provides functions for determining the pathname of the
17 current working directory. It is recommended that getcwd (or another
18 *cwd() function) be used in all code to ensure portability.
19
20 By default, it exports the functions cwd(), getcwd(), fastcwd(), and
21 fastgetcwd() (and, on Win32, getdcwd()) into the caller's namespace.
22
23 getcwd and friends
24 Each of these functions are called without arguments and return the
25 absolute path of the current working directory.
26
27 getcwd
28 my $cwd = getcwd();
29
30 Returns the current working directory. On error returns "undef",
31 with $! set to indicate the error.
32
33 Exposes the POSIX function getcwd(3) or re-implements it if it's
34 not available.
35
36 cwd
37 my $cwd = cwd();
38
39 The cwd() is the most natural form for the current architecture.
40 For most systems it is identical to `pwd` (but without the trailing
41 line terminator).
42
43 fastcwd
44 my $cwd = fastcwd();
45
46 A more dangerous version of getcwd(), but potentially faster.
47
48 It might conceivably chdir() you out of a directory that it can't
49 chdir() you back into. If fastcwd encounters a problem it will
50 return undef but will probably leave you in a different directory.
51 For a measure of extra security, if everything appears to have
52 worked, the fastcwd() function will check that it leaves you in the
53 same directory that it started in. If it has changed it will "die"
54 with the message "Unstable directory path, current directory
55 changed unexpectedly". That should never happen.
56
57 fastgetcwd
58 my $cwd = fastgetcwd();
59
60 The fastgetcwd() function is provided as a synonym for cwd().
61
62 getdcwd
63 my $cwd = getdcwd();
64 my $cwd = getdcwd('C:');
65
66 The getdcwd() function is also provided on Win32 to get the current
67 working directory on the specified drive, since Windows maintains a
68 separate current working directory for each drive. If no drive is
69 specified then the current drive is assumed.
70
71 This function simply calls the Microsoft C library _getdcwd()
72 function.
73
74 abs_path and friends
75 These functions are exported only on request. They each take a single
76 argument and return the absolute pathname for it. If no argument is
77 given they'll use the current working directory.
78
79 abs_path
80 my $abs_path = abs_path($file);
81
82 Uses the same algorithm as getcwd(). Symbolic links and relative-
83 path components ("." and "..") are resolved to return the canonical
84 pathname, just like realpath(3). On error returns "undef", with $!
85 set to indicate the error.
86
87 realpath
88 my $abs_path = realpath($file);
89
90 A synonym for abs_path().
91
92 fast_abs_path
93 my $abs_path = fast_abs_path($file);
94
95 A more dangerous, but potentially faster version of abs_path.
96
97 $ENV{PWD}
98 If you ask to override your chdir() built-in function,
99
100 use Cwd qw(chdir);
101
102 then your PWD environment variable will be kept up to date. Note that
103 it will only be kept up to date if all packages which use chdir import
104 it from Cwd.
105
107 • Since the path separators are different on some operating systems
108 ('/' on Unix, ':' on MacPerl, etc...) we recommend you use the
109 File::Spec modules wherever portability is a concern.
110
111 • Actually, on Mac OS, the "getcwd()", "fastgetcwd()" and "fastcwd()"
112 functions are all aliases for the "cwd()" function, which, on Mac
113 OS, calls `pwd`. Likewise, the "abs_path()" function is an alias
114 for "fast_abs_path()".
115
117 Originally by the perl5-porters.
118
119 Maintained by Ken Williams <KWILLIAMS@cpan.org>
120
122 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
123
124 This program is free software; you can redistribute it and/or modify it
125 under the same terms as Perl itself.
126
127 Portions of the C code in this library are copyright (c) 1994 by the
128 Regents of the University of California. All rights reserved. The
129 license on this code is compatible with the licensing of the rest of
130 the distribution - please see the source code in Cwd.xs for the
131 details.
132
134 File::chdir
135
136
137
138perl v5.34.0 2022-01-21 Cwd(3)