Minimal Perl for Unix and Linux People | Page 5
[previous]
6.3.2 Finding files by pathname matching
You've seen that Perl can mimic the behavior of find
's Âname
option by matching within a pathname's final segment. But Perl can do something the POSIX find can't doÂit can match anywhere within the pathname.
I use this feature periodically when I need to find one of my scripts by name. Because I know it resides in one of my *bin directories, I can use this knowledge to avoid matches with like-named files that reside in other directories, such as *man
and *lib
.
For instance, here's how Homer would locate his scripts for converting images in other formats to JPEGs, using the fact that he employs a *2jpg
convention in naming them:
Notice that the mX
syntax of the matching operator is used to specify the ":
" character as the delimiter, overriding the default of the slash. This allows the slash at the end of "bin/
" to unambiguously represent the directory separator in the pathname, which creates a context that ensures the \w+2jpg$
pattern is only matched as a whole filename under a *bin directory. By using this technique of matching filenames only within directories matching specified patterns, Homer can avoid undesirable matches such as these:
Next, you'll see how to use a special kind of find | perl
pipeline for filtering out undesirable arguments for Unix utilities, an d how to use Unix utilities for validating arguments for Perl programs.
Excerpted from Minimal Perl for Unix and Linux People by Tim Maher. Copyright © 2006. Used with permission of Manning Publications Co..
[previous]
URL: