Perl Modules about File

文件存取與處理 

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


  1. wanted : The value should be a code reference. This code reference is described in the wanted function , The &wanted subroutine is mandatory
  2. 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().
  3. 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::dirIt 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(空操做).
  4. postprocessThe 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.
  5. followCauses symbolic links to be followed
  6. follow_fast:

File::Password 對象

相關文章
相關標籤/搜索