文件存取与处理
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.
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/perluse 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 "*";}
finddepth 是先内容后目录,从叶子往树根。
wanted函数是一个回调函数。回调函数是指:一般不自己调用而是由别的程序调用,这里是由find调用。find两个参数第一个是对遍历到文件的行为,一个是待查找的目录。
%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.
- 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