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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|