x
Loading
 Loading
Hello, Guest | Login | Register

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.

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:

$ rm *.c 

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:

$…

Please log in to view this content.

Not Yet a Member?

Register with LinuxMagazine.com and get free access to the entire archive, including:

  • Hands-on Content
  • White Papers
  • Community Features
  • And more.
Already a Member?
Log in!
Username

Password

Remember me

Forgotten your password?
Forgotten your username?
Read More
  1. Got Security? You're in Denial
  2. KDE 4.4: Does It Work Yet?
  3. Writing Custom Nagios Plugins with Python
  4. Power Up Linux GUI Apps
  5. Tweeting from the Command Line with Twyt
Follow Linux Magazine
Rackspace