I have a need for a function in my program that would return back all xml-files it can find. The function needs to be recursive, because it goes through directory like this:
archive .2005 ..12 ...420.xml ..11 ...419.xml ..10 ...418.xml ...417.xml .2004 ..9 ...416.xml ..7 ...415.xml .2003 ..5 ...414.xml ..4 ...413.xml ..2 ...412.xml
So directory 'archive' has subdirs based on years and they have subdirs based on months, latter ones then have these xml-files that i would like to read on to array. When the funtion has read recursively all subdirs and pushed xml-files to array, it then returns that array. Where i'm facing problem, is with this code that i've copied from web and modified to suit my needs:
#!/usr/bin/perl my @files; loopDir('./'); print "found xml-files $#files..\n"; sub loopDir { local ($dir) = @_; chdir $dir or die "Cannot chdir to $dir\n"; local(*DIR); opendir DIR, '.'; while ($f=readdir(DIR)) { next if $f =~ /^\./; push @files, $dir . '/' . $f if $f =~ /.*\.xml$/; if (-d $f) { &loopDir($f); } } closedir(DIR); chdir("..");
There doesn't seem to be any other way to introduce that '@files' array, other than higher in the program. I would like this loopDir function to return it, not modify elsewhere introduced array. Like this i mean: my @files = loopDir('./');

In reply to Recursive file search by mellin

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.