⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 find.pm

📁 MSYS在windows下模拟了一个类unix的终端
💻 PM
📖 第 1 页 / 共 3 页
字号:
package File::Find;use strict;use warnings;use 5.6.0;our $VERSION = '1.01';require Exporter;require Cwd;=head1 NAMEfind - traverse a file treefinddepth - traverse a directory structure depth-first=head1 SYNOPSIS    use File::Find;    find(\&wanted, '/foo', '/bar');    sub wanted { ... }    use File::Find;    finddepth(\&wanted, '/foo', '/bar');    sub wanted { ... }    use File::Find;    find({ wanted => \&process, follow => 1 }, '.');=head1 DESCRIPTIONThe first argument to find() is either a hash reference describing theoperations to be performed for each file, or a code reference.Here are the possible keys for the hash:=over 3=item C<wanted>The value should be a code reference.  This code reference is calledI<the wanted() function> below.=item C<bydepth>Reports the name of a directory only AFTER all its entrieshave been reported.  Entry point finddepth() is a shortcut forspecifying C<{ bydepth => 1 }> in the first argument of find().=item C<preprocess>The value should be a code reference. This code reference is used to preprocess the current directory. The name of 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 I<follow> or I<follow_fast> are in effect, C<preprocess> is a no-op.=item C<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 I<follow> or I<follow_fast> are in effect, C<postprocess> is a no-op.=item C<follow>Causes symbolic links to be followed. Since directory trees with symboliclinks (followed) may contain files more than once and may even havecycles, a hash has to be built up with an entry for each file.This might be expensive both in space and time for a largedirectory tree. See I<follow_fast> and I<follow_skip> below.If either I<follow> or I<follow_fast> is in effect:=over 6=item *It is guaranteed that an I<lstat> has been called before the user'sI<wanted()> function is called. This enables fast file checks involving S< _>.=item *There is a variable C<$File::Find::fullname> which holds the absolutepathname of the file with all symbolic links resolved=back=item C<follow_fast>This is similar to I<follow> except that it may report some files morethan once.  It does detect cycles, however.  Since only symbolic linkshave to be hashed, this is much cheaper both in space and time.  Ifprocessing a file more than once (by the user's I<wanted()> function)is worse than just taking time, the option I<follow> should be used.=item C<follow_skip>C<follow_skip==1>, which is the default, causes all files which areneither directories nor symbolic links to be ignored if they are aboutto be processed a second time. If a directory or a symbolic link are about to be processed a second time, File::Find dies.C<follow_skip==0> causes File::Find to die if any file is about to beprocessed a second time.C<follow_skip==2> causes File::Find to ignore any duplicate files anddirectories but to proceed normally otherwise.=item C<dangling_symlinks>If true and a code reference, will be called with the symbolic linkname and the directory it lives in as arguments.  Otherwise, if trueand warnings are on, warning "symbolic_link_name is a danglingsymbolic link\n" will be issued.  If false, the dangling symbolic linkwill be silently ignored.=item C<no_chdir>Does not C<chdir()> to each directory as it recurses. The wanted()function will need to be aware of this, of course. In this case,C<$_> will be the same as C<$File::Find::name>.=item C<untaint>If find is used in taint-mode (-T command line switch or if EUID != UIDor if EGID != GID) then internally directory names have to be untaintedbefore they can be chdir'ed to. Therefore they are checked against a regularexpression I<untaint_pattern>.  Note that all names passed to the user's I<wanted()> function are still tainted. If this option is used while not in taint-mode, C<untaint> is a no-op.=item C<untaint_pattern>See above. This should be set using the C<qr> quoting operator.The default is set to  C<qr|^([-+@\w./]+)$|>. Note that the parantheses are vital.=item C<untaint_skip>If set, a directory which fails the I<untaint_pattern> is skipped, including all its sub-directories. The default is to 'die' in such a case.=backThe wanted() function does whatever verifications you want.C<$File::Find::dir> contains the current directory name, and C<$_> thecurrent filename within that directory.  C<$File::Find::name> containsthe complete pathname to the file. You are chdir()'d toC<$File::Find::dir> when the function is called, unless C<no_chdir>was specified.  When C<follow> or C<follow_fast> are in effect, there isalso a C<$File::Find::fullname>.  The function may setC<$File::Find::prune> to prune the tree unless C<bydepth> wasspecified.  Unless C<follow> or C<follow_fast> is specified, forcompatibility reasons (find.pl, find2perl) there are in addition thefollowing globals available: C<$File::Find::topdir>,C<$File::Find::topdev>, C<$File::Find::topino>,C<$File::Find::topmode> and C<$File::Find::topnlink>.This library is useful for the C<find2perl> tool, which when fed,    find2perl / -name .nfs\* -mtime +7 \        -exec rm -f {} \; -o -fstype nfs -pruneproduces 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);    }Set the variable C<$File::Find::dont_use_nlink> if you're using AFS,since AFS cheats.Here's another interesting wanted function.  It will find all symlinksthat don't resolve:    sub wanted {         -l && !-e && print "bogus link: $File::Find::name\n";    }See also the script C<pfind> on CPAN for a nice application of thismodule.=head1 CAVEATBe aware that the option to follow symbolic links can be dangerous.Depending on the structure of the directory tree (including symboliclinks to directories) you might traverse a given (physical) directorymore than once (only if C<follow_fast> is in effect). Furthermore, deleting or changing files in a symbolically linked directorymight cause very unpleasant surprises, since you delete or change filesin an unknown directory.=head1 NOTES=over 4=item *Mac OS (Classic) users should note a few differences:=over 4=item *   The path separator is ':', not '/', and the current directory is denoted as ':', not '.'. You should be careful about specifying relative pathnames. While a full path always begins with a volume name, a relative pathname should always begin with a ':'.  If specifying a volume name only, a trailing ':' is required.=item *   C<$File::Find::dir> is guaranteed to end with a ':'. If C<$_> contains the name of a directory, that name may or may not end with a ':'. Likewise, C<$File::Find::name>, which contains the complete pathname to that directory, and C<$File::Find::fullname>, which holds the absolute pathname of that directory with all symbolic links resolved,may or may not end with a ':'.=item *   The default C<untaint_pattern> (see above) on Mac OS is set to  C<qr|^(.+)$|>. Note that the parentheses are vital.=item *   The invisible system file "Icon\015" is ignored. While this file may appear in every directory, there are some more invisible system files on every volume, which are all located at the volume root level (i.e. "MacintoshHD:"). These system files are B<not> excluded automatically. Your filter may use the following code to recognize invisible files or directories (requires Mac::Files): use Mac::Files; # invisible() --  returns 1 if file/directory is invisible,   # 0 if it's visible or undef if an error occured sub invisible($) {    my $file = shift;   my ($fileCat, $fileInfo);    my $invisible_flag =  1 << 14;    if ( $fileCat = FSpGetCatInfo($file) ) {     if ($fileInfo = $fileCat->ioFlFndrInfo() ) {       return (($fileInfo->fdFlags & $invisible_flag) && 1);     }   }   return undef; }Generally, invisible files are system files, unless an odd application decides to use invisible files for its own purposes. To distinguish such files from system files, you have to look at the B<type> and B<creator> file attributes. The MacPerl built-in functions C<GetFileInfo(FILE)> and C<SetFileInfo(CREATOR, TYPE, FILES)> offer access to these attributes (see MacPerl.pm for details).Files that appear on the desktop actually reside in an (hidden) directorynamed "Desktop Folder" on the particular disk volume. Note that, althoughall desktop files appear to be on the same "virtual" desktop, each disk volume actually maintains its own "Desktop Folder" directory.=back=back=head1 HISTORYFile::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.=cutour @ISA = qw(Exporter);our @EXPORT = qw(find finddepth);use strict;my $Is_VMS;my $Is_MacOS;require File::Basename;require File::Spec;# Should ideally be my() not our() but local() currently# refuses to operate on lexicalsour %SLnkSeen;our ($wanted_callback, $avoid_nlink, $bydepth, $no_chdir, $follow,    $follow_skip, $full_check, $untaint, $untaint_skip, $untaint_pat,    $pre_process, $post_process, $dangling_symlinks);sub contract_name {    my ($cdir,$fn) = @_;    return substr($cdir,0,rindex($cdir,'/')) if $fn eq $File::Find::current_dir;    $cdir = substr($cdir,0,rindex($cdir,'/')+1);    $fn =~ s|^\./||;    my $abs_name= $cdir . $fn;    if (substr($fn,0,3) eq '../') {       1 while $abs_name =~ s!/[^/]*/\.\./!/!;    }    return $abs_name;}# return the absolute name of a directory or filesub contract_name_Mac {    my ($cdir,$fn) = @_;     my $abs_name;    if ($fn =~ /^(:+)(.*)$/) { # valid pathname starting with a ':'	my $colon_count = length ($1);	if ($colon_count == 1) {	    $abs_name = $cdir . $2;	    return $abs_name;	}	else { 	    # need to move up the tree, but 	    # only if it's not a volume name	    for (my $i=1; $i<$colon_count; $i++) {		unless ($cdir =~ /^[^:]+:$/) { # volume name		    $cdir =~ s/[^:]+:$//;		}		else {		    return undef;		}	    }	    $abs_name = $cdir . $2;	    return $abs_name;	}    }    else {	# $fn may be a valid path to a directory or file or (dangling)	# symlink, without a leading ':'	if ( (-e $fn) || (-l $fn) ) {	    if ($fn =~ /^[^:]+:/) { # a volume name like DataHD:*		return $fn; # $fn is already an absolute path	    }	    else {		$abs_name = $cdir . $fn;		return $abs_name;	    }	}	else { # argh!, $fn is not a valid directory/file 	     return undef;	}    }}sub PathCombine($$) {    my ($Base,$Name) = @_;    my $AbsName;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -