File::Find

By eric

Description: File::Find is a pretty straightforward and useful module. I often find myself needing to hunt down a bunch of files or parse through a particular grouping of files. Because of TIMTOWTDI, I usually choose to use File::Find based on its ease of use.

CPAN: File::Find

Example 1:
Being a System’s Administrator, I am usually hunting for something in a logfile. Therefore, I like to parse through all the logs for a particular program or daemon that I can get my hands on.

# Always use these
use strict;
use warnings;
# Use the module itself
use File::Find;

# Declare your variables
my @files;
my $dir = "/var/log/";

 # Actually find the file
 #  -f tests to see if it is a file (not a device or symlink, etc)
 #  Matches the RE for "mail.log.*"
 #    in the directory $dir
 find(
   sub { push @files, $File::Find::name if -f && /mail\.log.*/ },
   $dir);

 # Iterate over the files and print those found
 for my $file (@files) {
   print "File: $file\\n";
 }

Follow My Travels

Buy My Book

Archives

  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006

writing