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

Perl-18---目录 *** 作函数,第1张

概述一、读取当前目录: 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  

一、读取当前目录:

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可以为字符串直接数,也可以是值为字符串的简单变量或表达式;

总结

以上是内存溢出为你收集整理的Perl-18---目录 *** 作函数全部内容,希望文章能够帮你解决Perl-18---目录 *** 作函数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1289239.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存