Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: unlinking old files

by athomason (Curate)
on Jul 21, 2000 at 12:08 UTC ( [id://23549]=note: print w/replies, xml ) Need Help??


in reply to unlinking old files

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.

Replies are listed 'Best First'.
RE: Re: unlinking old files
by lindex (Friar) on Jul 22, 2000 at 03:29 UTC
    Since its a cgi your might want use an error function instead of die
    to prevent an internal server error" and def use something other than
    die on the unlink, warn would be fine or just use the same error function.
    i.e. return the error rather than die with dump to stderr

    Just my input, ignorable :)



    lindex
    /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://23549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found