x
Loading
 Loading
Hello, Guest | Login | Register

Finding Files Incrementally

The traditional File::Find module included with Perl is nice. I use it frequently. However, it’s got this interesting feature — or rather limitation; it wants to be in control until it has found everything of interest. Now, that’s perfectly fine for most applications, but I’ve occasionally wanted to turn a hierarchical search “inside out.” That is, I’d set up the search, including the start directories and the “wanted” criteria, and then repeatedly call some function to get the “next” item of interest. This is similar to how you could call the find program externally:

The traditional File::Find module included with Perl is nice. I use it frequently. However, it’s got this interesting feature — or rather limitation; it wants to be in control until it has found everything of interest. Now, that’s perfectly fine for most applications, but I’ve occasionally wanted to turn a hierarchical search “inside out.” That is, I’d set up the search, including the start directories and the “wanted” criteria, and then repeatedly call some function to get the “next” item of interest. This is similar to how you could call the find program externally:

open FIND, “find /home -atime +60 -print|”; while (<FIND>) { chomp; printf “%s aged %d days using %d blocksn”, $_, -A $_, -s $_ / 1024; } 

Recently, I saw Mark-Jason Dominus, Perl hacker extraordinaire, give a talk on using streams and iterators; he pulled out an example that was very close to what I wanted. He showed off a subroutine that acted as a factory, generating subroutines that, when repeatedly invoked, provided the “next” file to be found in sequence. (It’s one of the examples from his forthcoming book, some of which can already be found at http://perl.plover.com/book/.) He then went on to show how to put this inside a filter to throw away the files that did not meet the necessary criteria.

I spent some time analyzing this and decided to take a different, but similar, approach. Invoking a subroutine repeatedly is functional but not a…

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