Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-23 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found