Wednesday, April 13, 2011

Printing all the files that have either ".rtf" and ".ftp"

# Enforce good programming practices by enabling compiler warnings and strict variable declarations.
use warnings;
use strict;

# Import the necessary modules. 
# File::Find::Rule makes it easy to search for files and directories.
# Data::Dumper is used to cleanly print out complex data structures (like arrays) for debugging.
use File::Find::Rule;
use Data::Dumper;

# Create a rule to search for specific files and store the results in the @files array.
my @files = File::Find::Rule->file()                     # Step 1: Tell the rule we only want to find regular files (no directories).
                            ->name( '*.rtf', '*.ftp' )   # Step 2: Filter by name. This will match any file ending in '.rtf' OR '.ftp'.
                            ->in( './' );                # Step 3: Execute the search starting in the current directory ('./') and all its subdirectories.

# Print the final array of found files to the screen. 
# We pass a reference to the array (\@files) to Dumper so it formats the output nicely.
print Dumper(\@files);

No comments: