x
Loading
 Loading
Hello, Guest | Login | Register

A Great Find

In last month’s column, I introduced the File::Find module that’s included as part of the core Perl distribution. File::Find provides a framework to recursively catalog or manipulate directories and their contents.

In last month’s column, I introduced the File::Find module that’s included as part of the core Perl distribution. File::Find provides a framework to recursively catalog or manipulate directories and their contents.

To use File::Find you have to create a wanted subroutine. For every file found in one or more starting directories, File::Find calls the wanted subroutine, letting it do the work of processing or skipping the file. Except for communication provided via the $File::Find::prune variable, the wanted subroutine’s output is ignored. This is called a callback model.

Recently, Richard Clamp was inspired to write a wrapper around File::Find called File::Find::Rule that turns the actions of descending into a directory into more of a filter model. A rule object is created and a series of methods are called against it to set up ever-narrowing filters, separating those items of interest from the rest.

For example, to create a rule to find only those files (and not directories or other things) that have been accessed at least 14 days ago, we create a filter with:

 use File::Find::Rule; my $filter = File::Find::Rule->new; 

Initially, this filter finds everything. So we must restrict it:

 $filter->file; # find only files $filter->atime(’>14′); # accessed more than 14 days ago 

Now we have $filter which rejects any entry that doesn’t meet both of the criteria. (The default connector is and, if you want to think of it as a boolean expression.) All that remains is to give…

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. KDE 4.4: Does It Work Yet?
  2. Writing Custom Nagios Plugins with Python
  3. Power Up Linux GUI Apps
  4. Tweeting from the Command Line with Twyt
  5. When Memory Serves You: Using ramfs and tmpfs
Follow Linux Magazine
Rackspace