Q:

PERL - issues extracting a file from directory/subdirectories/..?

Quick note: I've been stuck with this problem for quite a few days and I'm not necessarily hoping to find an answer, but any kind of help that might "enlighten" me. I would also like to mention that I am a beginner in Perl, so my knowledge is not very vast and in this case recursivity is not my forte. here goes:
What I would like my Perl script to do is the following:

take a directory as an argument
go into the directory that was passed and its subdirectories to find an *.xml file
store the full path of the found *.xml file into an array.

Below is the code that i have so far, but i haven't managed to make it work:
#! /usr/bin/perl -W

my $path;
process_files ($path);

sub process_files
{
    opendir (DIR, $path) or die "Unable to open $path: $!";

    my @files =
        # Third: Prepend the full path
        map { $path . '/' . $_ }
        # Second: take out '.' and '..'
        grep { !/^\.{1,2}$/ }
        # First: get all files
        readdir (DIR);

    closedir (DIR);

    for (@files)
    {
          if (-d $_)
          {            
            push @files, process_files ($_);
          }
          else
          {
             #analyse document
          }
    }
    return @files;
}

Anybody have any clues to point me in the right direction? Or an easier way to do it?
Thank you,
sSmacKk :D

A:

Sounds like you should be using File::Find. Its find subroutine will traverse a directory recursively.
use strict;
use warnings;
use File::Find;

my @files;
my $path = shift;
find(
    sub { (-f && /\.xml$/i) or return; 
           push @files, $File::Find::name; 
    }, $path);

The subroutine will perform whatever code it contains on the files it finds. This one simply pushes the XML file names (with full path) onto the @files array. Read more in the documentation for the File::Find module, which is a core module in perl 5.

