Perl File::Find Undocumented Globals
$File::Find is a powerful way for traversing a directory tree.
I came across the need to determine (inside the wanted
function) the top directory from which the recursive search started. This perldoc mentions $File::Find::topdir
which appears to contains exactly what I needed. So, the particular script I was writing now works.
However, I was curious is there more documentation on this and the other glob开发者_如何学Goals mentioned in the same paragraph:
$File::Find::topdir
$File::Find::topdev
$File::Find::topino
$File::Find::topmode
$File::Find::topnlink
$File::Find::fullname
$File::Find::prune
Or alternatively, could you guys comment on any of these globals if you have used it in your own script-writing ventures.
If the globals are not documented, they are not intended for public use and you better refrain from using it.
The top* variables seem to be some of the usual meta-data for a file, as returned by the stats() system call. (device, inode, mode (i.e. access rights), number of links)
$File::Find::prune is documented, and is very useful. It's like -prune option of Unix's find command, one sets it to say "don't descend into this directory".
File::Find's POD also documents fullname rather well:
- When "follow" or "follow_fast" are in effect, there is also a
$File::Find::fullname
. .... 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".
As for the others- Way back in the early 90's, before Perl5 came along with its handy lexical variables, objects, and closures (and before CPAN), perl shipped with File::Find's precursor "find.pl" - and hence all the package variables that File::Find still uses. For some reason the "top" variables were left in that poorly documented state.
When calling find(\&wanted, @directories_to_search)
, the modules sets $File::Find::topdir
to each directory in @directories_to_search
as it goes through them. The other top
* variables relate to the attributes of that "top-level" directory.
精彩评论