📄 cwd.pm
字号:
package Cwd;=head1 NAMECwd - get pathname of current working directory=head1 SYNOPSIS use Cwd; my $dir = getcwd; use Cwd 'abs_path'; my $abs_path = abs_path($file);=head1 DESCRIPTIONThis module provides functions for determining the pathname of thecurrent working directory. It is recommended that getcwd (or another*cwd() function) be used in I<all> code to ensure portability.By default, it exports the functions cwd(), getcwd(), fastcwd(), andfastgetcwd() (and, on Win32, getdcwd()) into the caller's namespace. =head2 getcwd and friendsEach of these functions are called without arguments and return theabsolute path of the current working directory.=over 4=item getcwd my $cwd = getcwd();Returns the current working directory.Exposes the POSIX function getcwd(3) or re-implements it if it's notavailable.=item cwd my $cwd = cwd();The cwd() is the most natural form for the current architecture. Formost systems it is identical to `pwd` (but without the trailing lineterminator).=item fastcwd my $cwd = fastcwd();A more dangerous version of getcwd(), but potentially faster.It might conceivably chdir() you out of a directory that it can'tchdir() you back into. If fastcwd encounters a problem it will returnundef but will probably leave you in a different directory. For ameasure of extra security, if everything appears to have worked, thefastcwd() function will check that it leaves you in the same directorythat it started in. If it has changed it will C<die> with the message"Unstable directory path, current directory changedunexpectedly". That should never happen.=item fastgetcwd my $cwd = fastgetcwd();The fastgetcwd() function is provided as a synonym for cwd().=item getdcwd my $cwd = getdcwd(); my $cwd = getdcwd('C:');The getdcwd() function is also provided on Win32 to get the current workingdirectory on the specified drive, since Windows maintains a separate currentworking directory for each drive. If no drive is specified then the currentdrive is assumed.This function simply calls the Microsoft C library _getdcwd() function.=back=head2 abs_path and friendsThese functions are exported only on request. They each take a singleargument and return the absolute pathname for it. If no argument isgiven they'll use the current working directory.=over 4=item abs_path my $abs_path = abs_path($file);Uses the same algorithm as getcwd(). Symbolic links and relative-pathcomponents ("." and "..") are resolved to return the canonicalpathname, just like realpath(3).=item realpath my $abs_path = realpath($file);A synonym for abs_path().=item fast_abs_path my $abs_path = fast_abs_path($file);A more dangerous, but potentially faster version of abs_path.=back=head2 $ENV{PWD}If you ask to override your chdir() built-in function, use Cwd qw(chdir);then your PWD environment variable will be kept up to date. Note thatit will only be kept up to date if all packages which use chdir importit from Cwd.=head1 NOTES=over 4=item *Since the path seperators are different on some operating systems ('/'on Unix, ':' on MacPerl, etc...) we recommend you use the File::Specmodules wherever portability is a concern.=item *Actually, on Mac OS, the C<getcwd()>, C<fastgetcwd()> and C<fastcwd()>functions are all aliases for the C<cwd()> function, which, on Mac OS,calls `pwd`. Likewise, the C<abs_path()> function is an alias forC<fast_abs_path()>.=back=head1 AUTHOROriginally by the perl5-porters.Maintained by Ken Williams <KWILLIAMS@cpan.org>=head1 COPYRIGHTCopyright (c) 2004 by the Perl 5 Porters. All rights reserved.This program is free software; you can redistribute it and/or modifyit under the same terms as Perl itself.Portions of the C code in this library are copyright (c) 1994 by theRegents of the University of California. All rights reserved. Thelicense on this code is compatible with the licensing of the rest ofthe distribution - please see the source code in F<Cwd.xs> for thedetails.=head1 SEE ALSOL<File::chdir>=cutuse strict;use Exporter;use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);$VERSION = '3.27';@ISA = qw/ Exporter /;@EXPORT = qw(cwd getcwd fastcwd fastgetcwd);push @EXPORT, qw(getdcwd) if $^O eq 'MSWin32';@EXPORT_OK = qw(chdir abs_path fast_abs_path realpath fast_realpath);# sys_cwd may keep the builtin command# All the functionality of this module may provided by builtins,# there is no sense to process the rest of the file.# The best choice may be to have this in BEGIN, but how to return from BEGIN?if ($^O eq 'os2') { local $^W = 0; *cwd = defined &sys_cwd ? \&sys_cwd : \&_os2_cwd; *getcwd = \&cwd; *fastgetcwd = \&cwd; *fastcwd = \&cwd; *fast_abs_path = \&sys_abspath if defined &sys_abspath; *abs_path = \&fast_abs_path; *realpath = \&fast_abs_path; *fast_realpath = \&fast_abs_path; return 1;}# If loading the XS stuff doesn't work, we can fall back to pure perleval { if ( $] >= 5.006 ) { require XSLoader; XSLoader::load( __PACKAGE__, $VERSION ); } else { require DynaLoader; push @ISA, 'DynaLoader'; __PACKAGE__->bootstrap( $VERSION ); }};# Must be after the DynaLoader stuff:$VERSION = eval $VERSION;# Big nasty table of function aliasesmy %METHOD_MAP = ( VMS => { cwd => '_vms_cwd', getcwd => '_vms_cwd', fastcwd => '_vms_cwd', fastgetcwd => '_vms_cwd', abs_path => '_vms_abs_path', fast_abs_path => '_vms_abs_path', }, MSWin32 => { # We assume that &_NT_cwd is defined as an XSUB or in the core. cwd => '_NT_cwd', getcwd => '_NT_cwd', fastcwd => '_NT_cwd', fastgetcwd => '_NT_cwd', abs_path => 'fast_abs_path', realpath => 'fast_abs_path', }, dos => { cwd => '_dos_cwd', getcwd => '_dos_cwd', fastgetcwd => '_dos_cwd', fastcwd => '_dos_cwd', abs_path => 'fast_abs_path', }, qnx => { cwd => '_qnx_cwd', getcwd => '_qnx_cwd', fastgetcwd => '_qnx_cwd', fastcwd => '_qnx_cwd', abs_path => '_qnx_abs_path', fast_abs_path => '_qnx_abs_path', }, cygwin => { getcwd => 'cwd', fastgetcwd => 'cwd', fastcwd => 'cwd', abs_path => 'fast_abs_path', realpath => 'fast_abs_path', }, epoc => { cwd => '_epoc_cwd', getcwd => '_epoc_cwd', fastgetcwd => '_epoc_cwd', fastcwd => '_epoc_cwd', abs_path => 'fast_abs_path', }, MacOS => { getcwd => 'cwd', fastgetcwd => 'cwd', fastcwd => 'cwd', abs_path => 'fast_abs_path', }, );$METHOD_MAP{NT} = $METHOD_MAP{MSWin32};$METHOD_MAP{nto} = $METHOD_MAP{qnx};# Find the pwd command in the expected locations. We assume these# are safe. This prevents _backtick_pwd() consulting $ENV{PATH}# so everything works under taint mode.my $pwd_cmd;foreach my $try ('/bin/pwd', '/usr/bin/pwd', '/QOpenSys/bin/pwd', # OS/400 PASE. ) { if( -x $try ) { $pwd_cmd = $try; last; }}my $found_pwd_cmd = defined($pwd_cmd);unless ($pwd_cmd) { # Isn't this wrong? _backtick_pwd() will fail if somenone has # pwd in their path but it is not /bin/pwd or /usr/bin/pwd? # See [perl #16774]. --jhi $pwd_cmd = 'pwd';}# Lazy-load Carpsub _carp { require Carp; Carp::carp(@_) }sub _croak { require Carp; Carp::croak(@_) }# The 'natural and safe form' for UNIX (pwd may be setuid root)sub _backtick_pwd { # Localize %ENV entries in a way that won't create new hash keys my @localize = grep exists $ENV{$_}, qw(PATH IFS CDPATH ENV BASH_ENV); local @ENV{@localize}; my $cwd = `$pwd_cmd`; # Belt-and-suspenders in case someone said "undef $/". local $/ = "\n"; # `pwd` may fail e.g. if the disk is full chomp($cwd) if defined $cwd; $cwd;}# Since some ports may predefine cwd internally (e.g., NT)# we take care not to override an existing definition for cwd().unless ($METHOD_MAP{$^O}{cwd} or defined &cwd) { # The pwd command is not available in some chroot(2)'ed environments my $sep = $Config::Config{path_sep} || ':'; my $os = $^O; # Protect $^O from tainting # Try again to find a pwd, this time searching the whole PATH. if (defined $ENV{PATH} and $os ne 'MSWin32') { # no pwd on Windows my @candidates = split($sep, $ENV{PATH}); while (!$found_pwd_cmd and @candidates) { my $candidate = shift @candidates; $found_pwd_cmd = 1 if -x "$candidate/pwd"; } } # MacOS has some special magic to make `pwd` work. if( $os eq 'MacOS' || $found_pwd_cmd ) { *cwd = \&_backtick_pwd; } else { *cwd = \&getcwd; }}if ($^O eq 'cygwin') { # We need to make sure cwd() is called with no args, because it's # got an arg-less prototype and will die if args are present. local $^W = 0; my $orig_cwd = \&cwd; *cwd = sub { &$orig_cwd() }}# set a reasonable (and very safe) default for fastgetcwd, in case it# isn't redefined later (20001212 rspier)*fastgetcwd = \&cwd;# A non-XS version of getcwd() - also used to bootstrap the perl build# process, when miniperl is running and no XS loading happens.sub _perl_getcwd{ abs_path('.');}# By John Bazik## Usage: $cwd = &fastcwd;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -