Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm using file find to search for a .pdf
use File::Find; find(\&cleanup, "test"); # Subroutine that determines whether we matched the file extensions. sub cleanup { if (/\.pdf$/) { print "$File::Find::name\n"; } }
This wroks great, but now I want to search for file name beginning with certain characters. Is that possible?

Replies are listed 'Best First'.
Re: using File::Find to search for a particular file
by wind (Priest) on Apr 15, 2011 at 16:37 UTC

    Just change or add to your regex:

    use File::Find; find(sub { print "$File::Find::name\n" if /^start/; }, "test");
Re: using File::Find to search for a particular file
by Marshall (Canon) on Apr 16, 2011 at 07:54 UTC
    This is what you need to modify in sub cleanup():
     if (/\.pdf$/)
     if (^abc.*\.pdf$/

    First regex requires ".pdf" at the end of the string.
    Second regex requires "abc" at the beginning, followed by maybe some chars followed by '.pdf'

    A "maximal match" will allow the most characters at the beginning of the pattern to match, while still allowing the last pattern to match.