Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613699
  • 博文数量: 90
  • 博客积分: 5111
  • 博客等级: 大校
  • 技术积分: 928
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-29 16:56
文章存档

2011年(15)

2010年(34)

2009年(19)

2008年(22)

我的朋友

分类: LINUX

2008-11-13 13:18:12

Perl用于操作目录的命令汇编(Commands for directory manipulation in Perl)


Perl最强大的功能来自正则表达式的威力,不可避免地要经常的操作文件和目录。虽然Perl目前对目录操作的支持还不够强大,但却是日常管理任务中不可或缺的部分。下面是Perl对目录所支持的操作命令,还未进行系统整理,先放出来。

1 概述

Perl支持的(几乎)全部目录操作命令:
opendir, closedir, readdir, rewinddir, seekdir,  telldir, chdir, mkdir, rmdir -d。

2 命令参考


-d  File is a directory.

 

opendir DIRHANDLE,EXPR
Opens a directory named EXPR for processing by readdir, telldir, seekdir, rewinddir,

and closedir. Returns true if successful. DIRHANDLE may be an expression

 whose value can be used as an indirect dirhandle, usually the real

 dirhandle name. If DIRHANDLE is an undefined scalar variable (or array

or hash element), the variable is assigned a reference to a new

anonymous dirhandle. DIRHANDLEs have their own namespace separate

 from FILEHANDLEs.

 

closedir DIRHANDLE
Closes a directory opened by opendir and returns the success of that

system call.

readdir DIRHANDLE
Returns the next directory entry for a directory opened by opendir. If used in list context, returns all the rest of the entries in the directory. If there are no more entries, returns an undefined value in scalar context or a null list in list context.

If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. Otherwise, because we didn't chdir there, it would have been testing the wrong file.

    opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
    @dots = grep { /^\./ && -f "$some_dir/$_" } readdir(DIR);
    closedir DIR;

 

rewinddir DIRHANDLE
Sets the current position to the beginning of the directory for the readdir routine on DIRHANDLE.

 

seekdir DIRHANDLE,POS
Sets the current position for the readdir routine on DIRHANDLE. POS must be a value returned by telldir. seekdir also has the same caveats about possible directory compaction as the corresponding system library routine.


telldir DIRHANDLE
Returns the current position of the readdir routines on DIRHANDLE. Value may be given to seekdir to access a particular location in a directory. telldir has the same caveats about possible directory compaction as the corresponding system library routine.



chdir EXPR
chdir FILEHANDLE
chdir DIRHANDLE
chdir
Changes the working directory to EXPR, if possible. If EXPR is omitted, changes to the directory specified by $ENV{HOME}, if set; if not, changes to the directory specified by $ENV{LOGDIR}. (Under VMS, the variable $ENV{SYS$LOGIN} is also checked, and used if it is set.) If neither is set, chdir does nothing. It returns true upon success, false otherwise. See the example under die.

On systems that support fchdir, you might pass a file handle or directory handle as argument. On systems that don't support fchdir, passing handles produces a fatal error at run time.

 

mkdir FILENAME,MASK
mkdir FILENAME
Creates the directory specified by FILENAME, with permissions specified by MASK (as modified by umask). If it succeeds it returns true, otherwise it returns false and sets $! (errno). If omitted, MASK defaults to 0777.

In general, it is better to create directories with permissive MASK, and let the user modify that with their umask, than it is to supply a restrictive MASK and give the user no way to be more permissive. The exceptions to this rule are when the file or directory should be kept private (mail files, for instance). The perlfunc(1) entry on umask discusses the choice of MASK in more detail.

Note that according to the POSIX 1003.1-1996 the FILENAME may have any number of trailing slashes. Some operating and filesystems do not get this right, so Perl automatically removes all trailing slashes to keep everyone happy.

 


rmdir FILENAME
rmdir
Deletes the directory specified by FILENAME if that directory is empty. If it succeeds it returns true, otherwise it returns false and sets $! (errno). If FILENAME is omitted, uses $_.




阅读(1544) | 评论(0) | 转发(0) |
0

上一篇:perl测试

下一篇:perl中使用递归 遍历目录

给主人留下些什么吧!~~