in reply to Re: How do I find and delete files based on age?
in thread How do I find and delete files based on age?
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:
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.
|
|---|