blog20100901

2013/08/20 - プログラミング言語 Perl にまつわる etc. - Perl monger
参考 : perldoc, perldoc.jp, search.cpan.org, perldoc.perl.org ...
「 初めての Perl 第 6 版 」(オライリー・ジャパン発行 ISBN978-4-87311-567-2) 」
「 続・初めての Perl 改訂版 」(オライリー・ジャパン発行 ISBN4-87311-305-9) 」
「 Effective Perl 第 2 版 」(翔泳社発行 ISBN978-4-7981-3981-4) 」 ... etc,.

Perl Perl_7 翻訳 ディストリビューション

Perl モジュール [翻訳] File::Find ドキュメント (d168)

目次 - Perl Index


Theme



Perl について、復習を兼ねて断片的な情報を掲載して行く連載その d168 回。

今回は、ディレクトリツリーを横断するモジュール「 File::Find 」のドキュメントを確認しました。

正確な内容は 原文 を確認してください。当記事の誤解や誤訳の指摘は歓迎しています。




名前 (NAME)




File::Find - Traverse a directory tree.


File::Find - ディレクトリツリーを横断する.


概要 (SYNOPSIS)




use File:Find;
find(\&wanted, @directories_to_search);
sub wanted { ... }

use File:Find;
finddepth(\&wanted, @directories_to_search);
sub wanted { ... }

use File::Find;
find({ wanted => \&process, follow => 1 }, '.');




説明 (DESCRIPTION)




These are functions for searching through directory trees doing work on each file found similar to the Unix find command. File::Find exports two functions, find and finddepth. They work similarly but have subtle differences.


これらは ディレクトリツリーを検索して見つかった各ファイルで仕事をするための関数です Unix find コマンドと同様に. File::Find は 2 つの関数をエクスポートします, find と finddepth です. それらは同じように動作しますが微妙な違いがあります.


find




find(\&wanted, @directories);
find(\%options, @directories);




find() does a depth-first search over the given @directories in the order they are given. For each file or directory found, it calls the &wanted subroutine. (See below for details on how to use the &wanted function). Additionally, for each directory found, it will chdir() into that directory and continue the search, invoking the &wanted function on each file or subdirectory in the directory.


find() は深さ優先探索 (depth-first search) します 所定の @directories をそれが与えれた順番に. 見つかったファイルやディレクトリそれぞれのために, それは &wanted サブルーチンを呼び出します. (&wanted 関数をどのように使うかの詳細は下を参照してください). 加えて, 見つかった各ディレクトリのために, それは chdir() してそのディレクトリに入り検索を続け, そのディレクトリで見つかったファイルやサブディレクトリそれぞれで &wanted 関数を呼び出します.


finddepth




finddepth(\&wanted, @directories);
finddepth(\%options, @directories);




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.


finddepth()find() と同じく機能します それがディレクトリ内のコンテンツのために &wanted 関数を呼び出した 後で ディレクトリのためにそれ (&wanted 関数) を呼び出すことを除いて. それ (finddepth()) はプレオーダートラバーサル (前順 (行きがけ) 走査) の代わりにポストオーダートラバーサル (後順 (帰りがけ) 走査) を実行し, ディレクトリツリーの下から上に作動し find() はツリーの上から下に作動します.

# ファイルツリーに対して find() は「上から下」に, finddepth() は「下から上」に移動しながらそれぞれのファイルに対して &wanted関数 を呼び出して処理する.


% オプション (%options)



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. The code reference is described in "The wanted function" below.

find() への最初の引数は どちらかです あなたの &wanted 関数へのコードリファレンス, またはハッシュリファレンスです 各ファイルのための実行される操作を記述した. コードリファレンスは下の "wanted 関数" で説明しています.


Here are the possible keys for the hash:


こちらがハッシュで利用可能なキーです:


wanted




The value should be a code reference. This code reference is described in "The wanted function" below. The &wanted subroutine is mandatory.


この値はコードリファレンスでなければなりません. このコードリファレンスは下の "wanted 関数" で説明しています. &wanted サブルーチンは必須です.


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 of find().


リポートします ディレクトリの名前を そのエントリ (# ファイル) のすべてのリポートの 後で のみ. エントリポイントの finddepth()find() の最初の引数で {bydepth => 1 } を指定するためのショートカットです.


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. Your preprocessing function is called after readdir(), but before the loop that calls the wanted() function. 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.


値はコードリファレンスでなければなりません. このコードリファレンスは現在のディレクトリのプリプロセス (# 前処理) に使われます. 現在処理されているディレクトリの名前は $File::Find::dir にあります. あなたのプリプロセスをする関数は readdir() の後で呼ばれますが, wanted() 関数が呼ばれるループの前です. それは文字列のリスト (実際はファイル/ディレクトリの名前) とともに呼ばれて文字列のリストを返すことが期待されます. このコードはファイル/ディレクトリ名のアルファベット順, 数字順のソート, それからディレクトリエントリをその名前だけに基づく除外に使われます. follow や follow_fast が有効なとき, preprocess は何もしません.


postprocess




The value should be a code reference. It is invoked just before leaving the currently processed directory. It is called in void context with no arguments. The name of the current directory is in $File::Find::dir. 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.


値はコードリファレンスでなければなりません. それは現在処理しているディレクトリを去る直前に呼び出されます. それは引数なしで void コンテキストで呼び出されます. 現在のディレクトリの名前は $File::Find::dir にあります. このフックはディレクトリをサマライズ (# 要約) するのに便利です, そのディスク使用量を計算するような. follow または follow_fast が有効なとき, postprocess は何もしません.


follow




Causes symbolic links to be followed. Since directory trees with symbolic links (followed) may contain files more than once and may even have cycles, a hash has to be built up with an entry for each file. This might be expensive both in space and time for a large directory tree. See "follow_fast" and "follow_skip" below. If either follow or follow_fast is in effect:


シンボリックリンクをたどらせます. シンボリックリンク (フォローされる) を伴うディレクトリツリーは複数のファイルを含む場合があり循環していることさえあるので, ハッシュは各ファイルのエントリで構築する必要があります. これは大きなディレクトリツリーではスペースと時間の両方で高くつく可能性があります. 下の "follow_fast" と "follow_skip" を参照してください. もし follow や follow_fast が有効なら:



  • It is guaranteed that an lstat has been called before the user's wanted() function is called. This enables fast file checks involving _. Note that this guarantee no longer holds if follow or follow_fast are not set.

  • There is a variable $File::Find::fullname which holds the absolute pathname of the file with all symbolic links resolved. If the link is a dangling symbolic link, then fullname will be set to undef.





  • それはユーザの wanted() 関数の前に lstat が呼ばれることが保証されています. これは _ を含む高速なファイルチェックを可能にします. もし follow や follow_fast gaセットされていないならもはやこの保証は保持されないことに注意してください.

  • すべてのシンボリックリンク解決されたファイルの絶対パスを保持した変数 $File::Find::fullname があります. もしリンクがぶらさがっているシンボリックリンクなら, fullname は undef にセットされます.



This is a no-op on Win32.


これは Win32 では何もしません.


follow_fast



This is similar to follow except that it may report some files more than once. It does detect cycles, however. Since only symbolic links have to be hashed, this is much cheaper both in space and time. If processing a file more than once (by the user's wanted() function) is worse than just taking time, the option follow should be used.


これは follow に似ています いくつかのファイルを複数回リポートするかもしれないことを除いて. しかしながら, それは循環を検知します. シンボリックリンクだけをハッシュにしなければならないので, これはスペースと時間の両方でかなり安価です. もし (ユーザの wanted() 関数により) 複数回のファイル処理がただ時間がかかる以上に悪いなら, オプション follow を使うとよいはずです.

This is a no-op on Win32.


これは Win32 では何もしません.


follow_skip



follow_skip==1, which is the default, causes all files which are neither directories nor symbolic links to be ignored if they are about to be processed a second time. If a directory or a symbolic link are about to be processed a second time, File::Find dies.


follow_skip==1, それがデフォルトで, ディレクトリかシンボリックリンクのどちらでもないすべてのファイルが無視されるようにします もしそれらが 2 回目に処理されるなら. もしディレクトリやシンボリックリンクが 2 回目に処理されるなら, File::Find は die します.

follow_skip==0 causes File::Find to die if any file is about to be processed a second time.


follow_skip==0 は File::Find を die させます もしあらゆるファイルが 2 回目に処理されるなら.

follow_skip==2 causes File::Find to ignore any duplicate files and directories but to proceed normally otherwise.


follow==2 は File::Find を重複するあらゆるファイルとディレクトリを無視するようにしますが それ以外は普通に処理します.


dangling_symlinks



Specifies what to do with symbolic links whose target doesn't exist. If true and a code reference, will be called with the symbolic link name and the directory it lives in as arguments. Otherwise, if true and warnings are on, a warning of the form "symbolic_link_name is a dangling symbolic link\n" will be issued. If false, the dangling symbolic link will be silently ignored.


ターゲットが存在しないシンボリックリンクの処理をどうするか指定します. もし true とコードリファレンスなら, シンボリックリンク名とそれがいるディレクトリを引数として呼び出されます. それ以外で, もし true と warnings が on なら, simbolic_link_name is a dangling symbolic link\n" の形式で warning がだされます. もし false なら, ぶら下がりのシンボリックリンクは静かに無視されます.


no_chdir



Does not chdir() to each directory as it recurses. The wanted() function will need to be aware of this, of course. In this case, $_ will be the same as $File::Find::name.


再帰的に各ディレクトリに chdir() しません. wanted() 関数はこれを認識している必要があります. この場合, $_$File::Find::name と同じになります.


untaint



If find is used in taint-mode (-T command line switch or if EUID != UID or if EGID != GID), then internally directory names have to be untainted before they can be chdir'd to. Therefore they are checked against a regular expression untaint_pattern. Note that all names passed to the user's wanted() function are still tainted. If this option is used while not in taint-mode, untaint is a no-op.


もし find が tain-mode ( -T コマンドラインスイッチか if EUID != UID や if EGID != GID) なら, 内部のディレクトリ名が汚染されないようにしなければなりません それらが chdir できるようになる前に. ですからそれらは正規表現 untaint_pattern に対してチェックされます. ユーザの wanted() 関数に渡されるすべての名前はまだ汚染されていることに注意してください. もしこのオプションが taint-mode でないときに使われると, untaint は何もしません.


untaint_pattern



See above. This should be set using the qr quoting operator. The default is set to qr|^([-+@\w./]+)$|. Note that the parentheses are vital.


上を参照してください. これは qr クォート演算子 (see 0x1e) を使ってセットしなければなりません. デフォルトは qr|^([-+@\w./]+)$| にセットされています. カッコが重要なことに注目してください.


untaint_skip



If set, a directory which fails the untaint_pattern is skipped, including all its sub-directories. The default is to die in such a case.


もしセットされると, untaint_pattern が失敗したディレクトリがスキップされます, そのすべてのサブディレクトリを含めて. デフォルトでそのようなケースでは die します.


wanted 関数 (The wanted function)



The wanted() function does whatever verifications you want on each file and directory. Note that despite its name, the wanted() function is a generic callback function, and does not tell File::Find if a file is "wanted" or not. In fact, its return value is ignored.


wanted() 関数は各ファイルとディレクトリであなたが望む確認を何でも行います. その名前にもかかわらず, wanted() 関数は汎用のコールバック関数で, ファイルが "wanted" かどうかは File::Find に伝えないことに注意してください. 実際, その戻り値は無視されます.

The wanted function takes no arguments but rather does its work through a collection of variables.


wanted 関数は引数を取らずむしろ変数のコレクションを通じて機能します.



$File::Find::dir is the current directory name,

$_ is the current filename within that directory

$File::Find::name is the complete pathname to the file.





$File::Find::dir は現在のディレクトリ名,

$_ はそのディレクトリ内の現在のファイル名

$File::Find::name はそのファイルへの完全なパス名 (# フルパス)



The above variables have all been localized and may be changed without affecting data outside of the wanted function.


上記の変数はすべてローカライズされてその wanted 関数の外側のデータに影響を与えることなく変更できます.

For example, when examining the file /some/path/foo.ext you will have:


例えば, あなたがファイル /some/path/foo.ext を検査するとき:


$File::Find::dir = /some/path/
$_ = foo.ext
$File::Find::name = /some/path/foo.ext



You are chdir()'d to $File::Find::dir when the function is called, unless no_chdir was specified. Note that when changing to directories is in effect, the root directory (/) is a somewhat special case inasmuch as the concatenation of $File::Find::dir, '/' and $_ is not literally equal to $File::Find::name. The table below summarizes all variants:


あなたはファンクションが呼ばたとき $File::Find::dir に chdir します, no_chdir が指定されていない限り. 注意してくださいディレクトリの変更が有効な時, root ディレクトリ (/) は少し特別なケースです $File::Find::dir, '/' と $_ の連結が $File::Find::name と文字列で等しくならないので.


$File::Find::name $File::Find::dir $_
default / / .
no_chdir=>0 /etc / etc
/etc/x /etc x
no_chdir=>1 / / /
/etc / /etc
/etc/x /etc /etc/x


When follow or follow_fast are in effect, there is also a $File::Find::fullname. The function may set $File::Find::prune to prune the tree unless bydepth was specified. Unless follow or follow_fast is specified, for compatibility reasons (find.pl, find2perl) there are in addition the following globals available: $File::Find::topdir, $File::Find::topdev, $File::Find::topino, $File::Find::topmode and $File::Find::topnlink.


followfollow_fast が有効なときは, $File::Find::fullname もあります. 関数はツリーを刈り込むために $File::Find::prune もセットできます bydepth が指定されていない限り. followfollow_fast が指定されていないなら, 互換性の理由 (find.pl, find2perl) があるので追加で次のグローバル変数が使えます:

$File::Find::topdir
$File::Find::topdev
$File::Find::topino
$File::Find::topmode
それから
$File::Find::toplik

This library is useful for the find2perl tool (distributed as part of the App-find2perl CPAN distribution), which when fed,


このライブラリはツール find2perl (CPAN ディストリビューション App-find2perl の一部として配布されている) で便利です, それに与えられると:


find2perl / -name .nfs\* -mtime +7 \
-exec rm -f {} \; -o -fstype nfs -prune



produces something like:


このようなものが生成されます:


sub wanted {
/^\.nfs.*\z/s &&
(($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &&
int(-M _) > 7 &&
unlink($_)
||
($nlink || (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_))) &&
$dev < 0 &&
($File::Find::prune = 1);
}



Notice the _ in the above int(-M _): the _ is a magical filehandle that caches the information from the preceding stat(), lstat(), or filetest.


上記 ind(-M _) の中の _ に注目してください: _ は情報をキャッシュする魔法のファイルハンドルです 先行する stat(), lstat(), または filetest からの.

Here's another interesting wanted function. It will find all symbolic links that don't resolve:


こちらは他の興味深い wanted 関数です. それは解決しないすべてのシンボリックリンクを見つけます:


sub wanted {
-l && !-e && print "bogus link: $File::Find::name\n";
}



Note that you may mix directories and (non-directory) files in the list of directories to be searched by the wanted() function.


あなたはディレクトリと (非ディレクトリの) ファイルをミックスできることに注目してください wanted() 関数で検索されるディレクトリのリストで.


find(\&wanted, "./foo", "./bar", "./baz/epsilon");



In the example above, no file in ./baz/ other than ./baz/epsilon will be evaluated by wanted().


上記の例では, ./baz/epsilon 以外の ./baz/ 内のファイルは評価されません wanted() によって.

See also the script pfind on CPAN for a nice application of this module.


CPAN の pfind スクリプトもまた参照してください このモジュールを使ったナイスなアプリケーションに関して.


WARNINGS



If you run your program with the -w switch, or if you use the warnings pragma, File::Find will report warnings for several weird situations. You can disable these warnings by putting the statement


もしあなたがあなたのプログラムで -w スイッチとともにプログラムを実行する, またはあなたが warnings プラグマを使うなら, File::Find はいくつかの状況で警告をリポートします. あなたはこのステートメントを置くことでこれらの警告を無効にできます


no warnings 'File::Find';



in the appropriate scope. See warnings for more info about lexical warnings.


適切なスコープで. warnings (JP)を参照してください レキシカル warnings についての詳細な情報のために.


警告 (CAVEAT)



$dont_use_nlink



You can set the variable $File::Find::dont_use_nlink to 0 if you are sure the filesystem you are scanning reflects the number of subdirectories in the parent directory's nlink count.


あなたは変数 $File::Find::dont_use_nlink を 0 にセットできます もしあなたがあなたがスキャンしているファイルシステムを確認したい場合 親ディレクトリの nlink カウントでサブディレクトリの数を反映しているか.

If you do set $File::Find::dont_use_nlink to 0, you may notice an improvement in speed at the risk of not recursing into subdirectories if a filesystem doesn't populate nlink as expected.


もしあなたが $File::Find::dont_use_nlink を 0 にセットするなら, あなたはスピードが改善していることに気がつくかもしれません ファイルシステムが期待どおりに nlink を追加しない場合にサブディレクトリに再帰しないリスクの上で.

$File::Find::dont_use_nlink now defaults to 1 on all platforms.


$File::Find::dont_use_nlink は現在すべてのプラットフォームで 1 がデフォルトです.


symlinks



Be aware that the option to follow symbolic links can be dangerous. Depending on the structure of the directory tree (including symbolic links to directories) you might traverse a given (physical) directory more than once (only if follow_fast is in effect). Furthermore, deleting or changing files in a symbolically linked directory might cause very unpleasant surprises, since you delete or change files in an unknown directory.

シンボリックリンクをたどるこのオプションがデンジャラスになりえることを認識しておいてください. ディレクトリツリーの構造 (ディレクトリへのシンボリックリンクを含む) によっては あなたは与えられた (物理的な) ディレクトリを (follow_fast が有効な場合のみ) 複数回横断するかもしれません. さらに, シンボリックリンクされたディレクトリないのファイルを削除したり変更するととても不快な驚きが生じるかもしれません, あなたは不明なディレクトリで削除や変更をするので.


バグと注意事項 (BUGS AND CAVEATS)



Despite the name of the finddepth() function, both find() and finddepth() perform a depth-first search of the directory hierarchy.


findpath() 関数の名前にもかかわらず, find()finddepth() はともにディレクトリ階層の深さ優先 (depth-first) 探索を実行します.

ヒストリ (HISTORY)



File::Find used to produce incorrect results if called recursively. During the development of perl 5.8 this bug was fixed. The first fixed version of File::Find was 1.01.


File::Find は再帰的に呼ばれた場合によく不正な結果を生成していました. perl 5.8 の開発をしている間でこのバグは fix されました. File::Find の最初の fix されたバージョンは 1.01 でした.


参照 (SEE ALSO)



find(1), find2perl




NEXT



次回は、実際に File::Find を使ってみます。


参考情報は書籍「 続・初めての Perl 改訂版 」, 「 Effective Perl 第 2 版 」を中心に perldoc, Wikipedia および各 Web サイト。それと詳しい先輩。

目次 - Perl Index
































同じカテゴリー(Perl)の記事
 Perl mp2 翻訳 Web コンテンツ圧縮の FAQ (d228) (2023-10-11 23:49)
 Perl mp2 翻訳 既知のブラウザのバグの回避策をいくつか (d227) (2023-05-26 15:41)
 Perl mp2 翻訳 Perl と Apache でのキュートなトリック (d226) (2023-05-19 17:05)
 Perl mp2 翻訳 テンプレートシステムの選択 (d225) (2022-08-15 22:23)
 Perl mp2 翻訳 大規模 E コマースサイトの構築 (d224) (2022-06-15 20:43)
 Perl mp2 翻訳 チュートリアル (d223) (2022-06-15 20:42)

Llama
リャマ
TI-DA
てぃーだブログ
プロフィール
セラ (perlackline)
セラ (perlackline)
QRコード
QRCODE
オーナーへメッセージ

PAGE TOP ▲