Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
use strict;

Why not

use warnings; # as well?
if ($ARGV[0] eq "") { $ARGV[0]="."; }

Later on you say: "If you don't specify a directory, it will use whatever the current directory is." Had you warnings turned on, this would trigger an 'uninitialized' warning. Which is sensible: actually $ARGV[0] would be undefined rather than strictly equal to the empty string. I would use the simpler

@ARGV = '.' unless @ARGV;

so that all the directories supplied on the command line would be searched, and a reasonable default would be provided if none is specified. Granted: this is not meant as a harsh critique to your code. I know it is just an example. I only want to expand a little bit on the subject.

my @file_list; find ( sub { my $file = $File::Find::name; if ( -f $file && $file =~ /^DATE_/) { push (@file_list, $file) } }, @ARGV);

Two things:

  1. I like to use File::Find's no_chdir mode, so that I wouldn't need $File::Find::name. As of now your code is actually wrong, since find() is changing dir, so that $file which is a path relative to the base dir being searched, will be interpreted relative to the cwd, and -f will most likely fail, except for coincidences;
  2. I used to write such code too, that first collects filenames, and then process them later. If huge volumes of files are to be skimmed through, though, this may make the script seemingly "hang" before it says something interesting. Thus nowadays I avoid doing so, if possible. In this particular case I see no reason why the check on the date couldn't be made in the sub that is supplied to find() in the first place. (Ok, the resulting code wouldn't do exactly the same as yours, the difference being given a few seconds or at most minutes whereas the threshold is measured in days - so I wouldn't regard it as significative.)
my @stats = stat($file); if ($now-$stats[9] > $AGE) { # file older than 14 days

I know you probably know, and include an intermediate passage for clarity and instructive purposes, but it is perhaps worth reminding that one can take a list slice as well, and that the temporary @stats variable is not needed:

if ($now-(stat $file)[9] > $AGE) { # file older than 14 days

BTW: I am the first one to say one shouldn't care about premature optimization, but stats are known to be expensive, and $file is already statted when it's being searched, so one more reason to do the check at find() time.


In reply to Re^2: How do I find and delete files based on age? by blazar
in thread How do I find and delete files based on age? by macvsog

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 meditating upon the Monastery: (4)
As of 2024-03-29 02:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found