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
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.
31
32 Exposes the POSIX function getcwd(3) or re-implements it if it's
33 not available.
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()
71 function.
72
73 abs_path and friends
74 These functions are exported only on request. They each take a single
75 argument and return the absolute pathname for it. If no argument is
76 given they'll use the current working directory.
77
78 abs_path
79 my $abs_path = abs_path($file);
80
81 Uses the same algorithm as getcwd(). Symbolic links and relative-
82 path components ("." and "..") are resolved to return the canonical
83 pathname, just like realpath(3).
84
85 realpath
86 my $abs_path = realpath($file);
87
88 A synonym for abs_path().
89
90 fast_abs_path
91 my $abs_path = fast_abs_path($file);
92
93 A more dangerous, but potentially faster version of abs_path.
94
95 $ENV{PWD}
96 If you ask to override your chdir() built-in function,
97
98 use Cwd qw(chdir);
99
100 then your PWD environment variable will be kept up to date. Note that
101 it will only be kept up to date if all packages which use chdir import
102 it from Cwd.
103
105 · Since the path seperators are different on some operating systems
106 ('/' on Unix, ':' on MacPerl, etc...) we recommend you use the
107 File::Spec modules wherever portability is a concern.
108
109 · Actually, on Mac OS, the "getcwd()", "fastgetcwd()" and "fastcwd()"
110 functions are all aliases for the "cwd()" function, which, on Mac
111 OS, calls `pwd`. Likewise, the "abs_path()" function is an alias
112 for "fast_abs_path()".
113
115 Originally by the perl5-porters.
116
117 Maintained by Ken Williams <KWILLIAMS@cpan.org>
118
120 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
121
122 This program is free software; you can redistribute it and/or modify it
123 under the same terms as Perl itself.
124
125 Portions of the C code in this library are copyright (c) 1994 by the
126 Regents of the University of California. All rights reserved. The
127 license on this code is compatible with the licensing of the rest of
128 the distribution - please see the source code in Cwd.xs for the
129 details.
130
132 File::chdir
133
134
135
136perl v5.12.4 2011-06-20 Cwd(3pm)