文件存取与处理
Cwd 取得现行工做目录的路径名
DirHandle 提供处理目录代码的对象方法
Fcntl 载入C的Fcntl.h中的定义
File::Basename 分割文件名数据
File::CheckTree 对一连串文件串作许多测试
File::Copy 拷贝文件或文件句柄
File::Find 寻找文件
File::Path 产生或移除一连串目录
FileCache 容许打开多于系统限制的文件句柄
FileHandle 提供处理文件句柄的对象方法
File::Find
Traverse a directory tree. 函数
find() does a depth-first search over the given @directories in the order they are given. post
finddepth() works just like find() except that it invokes the &wanted function for a directory after invoking it for the directory's contents. It does a postorder traversal instead of a preorder traversal, working from the bottom of the directory tree up where find() works from the top of the tree down. 测试
#!/usr/bin/perl
use warnings;
use strict;
use File::Find;
print "find():\n";
find(\&wanted,'.');
print "\n\n";
print "finddepth():\n"
finddepth(\&wanted,'.');
sub wanted {
print $File::Find::name,"\n" if ($_ =~ /^test/);
print "*";
}
其中有三个变量:
1.$File::Find::dir :当前处理的目录名
2.$_ :当前处理的文件名
3.$File::Find::name :目录加文件名
find是在进行目录测试后在进行目录内容的测试。从目录树根往下。
finddepth 是先内容后目录,从叶子往树根。 spa
wanted函数是一个回调函数。回调函数是指:通常不本身调用而是由别的程序调用,这里是由find调用。find两个参数第一个是对遍历到文件的行为,一个是待查找的目录。 code
%option
The first argument to find() is either a code reference to your &wanted function, or a hash reference describing the operations to be performed for each file. orm
- wanted : The value should be a code reference. This code reference is described in the wanted function , The &wanted subroutine is mandatory
- bydepth : Reports the name of a directory only AFTER all its entries have been reported. Entry point finddepth() is a shortcut for specifying { bydepth => 1 } in the first argument offind().
- preprocess : The value should be a code reference. This code reference is used to preprocess the current directory.The name of the currently processed directory is in $File::Find::dir. It is called with a list of strings (actually file/directory names) and is expected to return a list of strings.The code can be used to sort the file/directory names alphabetically, numerically, or to filter out directory entries based on their name alone. When follow or follow_fast are in effect, preprocess is a no-op(空操做).
- postprocess : The value should be a code reference. It is invoked just before leaving the currently processed directory.This hook is handy for summarizing a directory, such as calculating its disk usage. When follow or follow_fast are in effect, postprocess is a no-op.
- follow : Causes symbolic links to be followed
- follow_fast:
File::Password 对象