I'm trying to write a script that will find all files with a specific extension (.hhc in my case) under the current directory. I have some basic code working, but it's not quite what I want and I'm not sure where to go from here. I'm not having much luck with the documentation or Cookbook examples.

Here's the code so far...

use strict; use File::Find; use Cwd; # BEGIN MAIN $, = "\n"; print &getFileList; # END MAIN sub getFileList { my @fileList = (); # list for matching files find(\&findFilter, "."); # populates the list return @fileList; sub findFilter { if ( /\.hhc$/ ) { # only push matching filenames my $curDir = cwd; my $fileName = $curDir . $_; push @fileList, $fileName; } return; } }

Here are my questions and issues:

  1. I'm getting a warning about using @fileList in the sub-subroutine. I know that's bad form, but I couldn't figure out a better way to do this. Is there a better way?
  2. I'd like to extract paths relative to the starting directory. I tried to do this by passing cwd from getFileList to findFilter. However, whenever I pass parameters to fileFilter, it seems to lose the file from $_. Is there a way to pass arguments into fileFilter?

I had to do a lot of digging to get enough information to get this far and really appreciate any help you can offer. I'm also hoping this can serve as a useful example in case anyone else is trying to do the same thing. For example, this can also be a good way to create an MP3 playlist.

Thanks for your help everyone.


In reply to Using File::Find to Create a List of Files by svsingh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.