Getting Some Directory Assistance
Most Perl scripts aren’t doing anything glamorous. They’re the workhorse of your system, moving things around and handling those mundane repetitive tasks while you aren’t necessarily looking. Those tasks are often on a series of filenames, perhaps not known in advance but obtained by looking at the contents of a directory. Perl has a few primary means of getting lists of names, so let’s take a look at them.
Thursday, February 15th, 2001
Most Perl scripts aren’t doing anything glamorous. They’re the workhorse of your system, moving things around and handling those mundane repetitive tasks while you aren’t necessarily looking. Those tasks are often on a series of filenames, perhaps not known in advance but obtained by looking at the contents of a directory. Perl has a few primary means of getting lists of names, so let’s take a look at them.
The simplest to use and understand is globbing. Globbing is what the shell does when you use echo *.c to get a list of all the C source files in a directory. The term globbing comes from the use of the old /etc/glob program in early versions of Unix, with a name derived from something like “global expansion.”
Most programs running from the shell don’t have to know how to do globbing for themselves. For example, the rm command in:
never sees the *.c. Instead, the shell expands (globs) the filename pattern, comes up with a list of names, and then hands those names to the arguments of rm. This is why the rm command can’t help you when you’ve accidentally typed a space between the asterisk and the period; it never sees the asterisk, but rather sees a list of explicit names, just as if you’d laboriously typed all of them directly.
Similarly, if you invoke your Perl program with glob pattern on the command line: