A little more context would be useful, since cronjobs sound like the more appropriate solution here. Is there a reason you don't want to use them? Any code like this will put unnecessary load on your web server, since many redundant checks will be done. A cronjob would run the needed code exactly as often as would be needed. Just be sure you're using the right tool.
In any case, I'll assume for now that you have a good reason. Here's some code that would do what you want:
#!/usr/bin/perl -w
use strict;
use CGI::Carp;
my $dir = "/tmp/wwwtrash";
opendir DIR, "$dir" or die "Couldn't open directory $dir: $!";
my @files = grep { (-f "$dir/$_") && (-M "$dir/$_" > 1) } readdir(DIR)
+;
closedir DIR;
unlink @files or die "Couldn't unlink files in $dir: $!";
# rest of your script
Update:
As lindex pointed out, dying is bad CGI manners since it tends to put nothing useful in the logs or the user's browser. Add use CGI::Carp; after the strict pragma (done above) and eveything should be taken care of.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.