加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl-18---目录操作函数

发布时间:2020-12-16 00:43:39 所属栏目:大数据 来源:网络整理
导读:一、读取当前目录: NAME ??? Cwd - get pathname of current working directory SYNOPSIS ??????? use Cwd; ??????? my $dir = getcwd; ??????? use Cwd 'abs_path'; ??????? my $abs_path = abs_path($file); DESCRIPTION ??? This module provides functi

一、读取当前目录:

NAME
??? Cwd - get pathname of current working directory

SYNOPSIS
??????? use Cwd;
??????? my $dir = getcwd;

??????? use Cwd 'abs_path';
??????? my $abs_path = abs_path($file);

DESCRIPTION
??? This module provides functions for determining the pathname of the
??? current working directory. It is recommended that getcwd (or another
??? *cwd() function) be used in *all* code to ensure portability.

??? By default,it exports the functions cwd(),getcwd(),fastcwd(),and
??? fastgetcwd() (and,on Win32,getdcwd()) into the caller's namespace.

? getcwd and friends
??? Each of these functions are called without arguments and return the
??? absolute path of the current working directory.

??? getcwd
??????????? my $cwd = getcwd();

??????? Returns the current working directory.

??????? Exposes the POSIX function getcwd(3) or re-implements it if it's not
??????? available.

??? cwd
??????????? my $cwd = cwd();

??????? The cwd() is the most natural form for the current architecture. For
??????? most systems it is identical to `pwd` (but without the trailing line
??????? terminator).

??? 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't
??????? chdir() you back into. If fastcwd encounters a problem it will
??????? return undef but will probably leave you in a different directory.
??????? For a measure of extra security,if everything appears to have
??????? worked,the fastcwd() function will check that it leaves you in the
??????? same directory that it started in. If it has changed it will "die"
??????? with the message "Unstable directory path,current directory changed
??????? unexpectedly". That should never happen.

??? fastgetcwd
????????? my $cwd = fastgetcwd();

??????? The fastgetcwd() function is provided as a synonym for cwd().

??? getdcwd
??????????? my $cwd = getdcwd();
??????????? my $cwd = getdcwd('C:');

??????? The getdcwd() function is also provided on Win32 to get the current
??????? working directory on the specified drive,since Windows maintains a
??????? separate current working directory for each drive. If no drive is
??????? specified then the current drive is assumed.

??????? This function simply calls the Microsoft C library _getdcwd()
??????? function.

? abs_path and friends
??? These functions are exported only on request. They each take a single
??? argument and return the absolute pathname for it. If no argument is
??? given they'll use the current working directory.

??? abs_path
????????? my $abs_path = abs_path($file);

??????? Uses the same algorithm as getcwd(). Symbolic links and
??????? relative-path components ("." and "..") are resolved to return the
??????? canonical pathname,just like realpath(3).

??? realpath
????????? my $abs_path = realpath($file);

??????? A synonym for abs_path().

??? fast_abs_path
????????? my $abs_path = fast_abs_path($file);

??????? A more dangerous,but potentially faster version of abs_path.

? $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 that it
??? will only be kept up to date if all packages which use chdir import it
??? from Cwd.

NOTES
??? *?? Since the path seperators are different on some operating systems
??????? ('/' on Unix,':' on MacPerl,etc...) we recommend you use the
??????? File::Spec modules wherever portability is a concern.

??? *?? Actually,on Mac OS,the "getcwd()","fastgetcwd()" and "fastcwd()"
??????? functions are all aliases for the "cwd()" function,which,on Mac
??????? OS,calls `pwd`. Likewise,the "abs_path()" function is an alias for
??????? "fast_abs_path()".

AUTHOR
??? Originally by the perl5-porters.

??? Maintained by Ken Williams <KWILLIAMS@cpan.org>

COPYRIGHT
??? Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.

??? This program is free software; you can redistribute it and/or modify it
??? under the same terms as Perl itself.

??? Portions of the C code in this library are copyright (c) 1994 by the
??? Regents of the University of California. All rights reserved. The
??? license on this code is compatible with the licensing of the rest of the
??? distribution - please see the source code in Cwd.xs for the details.

SEE ALSO
??? File::chdir

eg:

一、mkdir()函数:

该函数用于创建一个新目录;调用语法:

$Ret? =? mkdir(DirName,Permissions);

DirName:这个参数指定需要创建的目录名;可以为字符串直接数或表达式;

Permissions:这个参数指定新创建的目录DirName的访问权限;该参数为八进制数;

参数Permissions设定的目录权限值:

调用成功,函数返回非零值(真:1),调用失败,函数返回零值(假);
也就是说,创建目录成功,则函数返回非零值(真:1),创建目录失败,则函数返回零值(假:0);

权限值?????? 权限描述

4000?????????? 运行时设置用户ID

2000?????????? 运行时设定组ID

1000?????????? 粘贴位

0400???????????拥有者读权限

0200?????????? 拥有者写权限

0100???????????拥有者执行权限

0040?????????? 同组用户读权限

0020?????????? 同组用户写权限

0010?????????? 同组用户执行权限

0004?????????? 其它组用户读权限

0002?????????? 其它组用户写权限

0001?????????? 其它组用户执行权限

二、rmdir()函数

$Ret?? =? rmdir(DirName);

该函数用于删除指定的目录,但是这个目录必须是空目录;

调用成功,函数返回非零值(真:1),调用失败,函数返回零值(假:0);
也就是说,创建目录成功,则函数返回非零值(真:1),创建目录失败,则函数返回零值(假:0);

三、opendir()函数:

$Ret?? =? opendir(DirHandle,DirName);

类似于Unix/Linux下的C函数opder();用于打开目录文件,以供以后读取目录文件中的内容;

DirHandle:目录句柄,与文件句柄类似;

DirName:目录名,可以为字符串或表达式;

调用成功,函数返回非零值(真:1),调用失败,函数返回空值(假);
也就是说,打开目录成功,则函数返回非零值(真:1),打开目录失败,则函数返回空值(假);

注意:程序中可以使用同名的目录句柄和文件句柄,Perl会根据其上下文环境来确定其类型;

四、closedir()函数

$Ret? =? closedir(DirHandle);

用于关闭已经打开的目录句柄DirHandle;与opendir()配合使用;

调用成功,函数返回非零值(真:1),调用失败,函数返回空值(假);
也就是说,关闭目录成功,则函数返回非零值(真:1),关闭目录失败,则函数返回空值(假);

五、readdir()函数

$FileName? =? readdir(DirHandle);

?或

@FileNames? =? readdir(DirHandle);

该函数用于读取目录文件中的内容,一般位文件名或其子目录下的文件名;

调用成功,则依据等号左边是简单的标量变量还是数组分别返回简单的标量值或数组值;调用失败则返回空值(假:0);也就是说,读取目录文件成功,则依据等号左边的变量是简单的标量变量还是数组分别返回简单的标量值或数组值;读取目录文件失败,则返回空值(假);

六、telldir() 函数:

$Position? =? telldir(DirHandle) ;

这个函数与文件操作的tell()函数的功能类似;用于在目录文件头和目录文件尾部之间移动目录文件读位置指针;

调用成功,则返回目录文件的读位置指针所在的当前位置(从目录文件起始位置处到目录文件读位置指针当前位置处的距离:元素个数);调用失败,则返回空值(假);

七、seekdir()函数

$Ret??=? seekdir(DirHandle,Location);

该函数在目录文件起始位置与尾部位置之间随意地移动目录文件读位置指针;

参数Location必须是telldir()函数的返回值;

调用成功,则返回非零值(真:1);调用失败,则返回空值(假);

八、rewinddir()函数

$Ret? =? rewinddir(DirHandle);

该函数用于把读取目录文件的读位置指针从新放回到目录文件的起始位置处,从而可以重新读取目录列表;

调用成功,则返回非零值(真:1);调用失败,则返回空值(假);

九、chdir()函数:

$Ret? = chdir(DirName);

调用成功,函数返回非零值(真:1),调用失败,函数返回零值(假:0);
也就是说,更改工作目录成功,则函数返回非零值(真:1),更改工作目录失败,则函数返回零值(假:0);

用于改变当前工作目录为参数DirNamer所指定的目录;参数DirName可以为字符串直接数,也可以是值为字符串的简单变量或表达式;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读