I think the code below looks cleaner. Depending on what your code does, you can use the memoize module to keep a cache of files you have already checked. Passing $dir_path and $filepath to the subroutine is definitely a good idea. My point here is that it is best to keep subroutines self-contained and not use external variables:
use strict; use Memoize; memoize ('read_files'); sub process_files { my $dir_path = shift; if (opendir(TEST, $dir_path)) { my @files = sort grep{$_ ne '.' and $_ ne '..'} readdir(TEST); #print "\n@files[0]\n"; read_files("$dir_path\\$_") foreach (@files); } else { die ("Could not Opendir $dir_path: $!\n"); }closedir TEST; } sub read_files { my $file_path = shift; if (-f $file_path) { print (DATA "$file_path\n") if ($file_path !~ /(\.lfa|\.zip|\. +txt|UASTG)$/); } else { die ("Could not open[$file_path], $!\n"); } }
The only global here is the DATA filehandle, but you should pass that along through the subroutine chain as a parameter.
-imran

In reply to Re^2: Help with a faster loop by CountOrlok
in thread Help with a faster loop by gzayzay

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.