1Cwd(3pm) Perl Programmers Reference Guide Cwd(3pm)
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 cur‐
17 rent 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
25 Each of these functions are called without arguments and return the
26 absolute path of the current working directory.
27
28 getcwd
29 my $cwd = getcwd();
30
31 Returns the current working directory.
32
33 Re-implements the getcwd(3) (or getwd(3)) functions in Perl.
34
35 cwd
36 my $cwd = cwd();
37
38 The cwd() is the most natural form for the current architecture.
39 For most systems it is identical to `pwd` (but without the trailing
40 line terminator).
41
42 fastcwd
43 my $cwd = fastcwd();
44
45 A more dangerous version of getcwd(), but potentially faster.
46
47 It might conceivably chdir() you out of a directory that it can't
48 chdir() you back into. If fastcwd encounters a problem it will
49 return undef but will probably leave you in a different directory.
50 For a measure of extra security, if everything appears to have
51 worked, the fastcwd() function will check that it leaves you in the
52 same directory that it started in. If it has changed it will "die"
53 with the message "Unstable directory path, current directory
54 changed unexpectedly". That should never happen.
55
56 fastgetcwd
57 my $cwd = fastgetcwd();
58
59 The fastgetcwd() function is provided as a synonym for cwd().
60
61 getdcwd
62 my $cwd = getdcwd();
63 my $cwd = getdcwd('C:');
64
65 The getdcwd() function is also provided on Win32 to get the current
66 working directory on the specified drive, since Windows maintains a
67 separate current working directory for each drive. If no drive is
68 specified then the current drive is assumed.
69
70 This function simply calls the Microsoft C library _getdcwd() func‐
71 tion.
72
73 abs_path and friends
74
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).
85
86 realpath
87 my $abs_path = realpath($file);
88
89 A synonym for abs_path().
90
91 fast_abs_path
92 my $abs_path = fast_abs_path($file);
93
94 A more dangerous, but potentially faster version of abs_path.
95
96 $ENV{PWD}
97
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 seperators 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.8.8 2001-09-21 Cwd(3pm)