Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am creating a classifieds script... I am making it so posts after seven days of age, will expire.
What would be the best way to do this? There is nothing that.. after seven days exactly, the file will just dissapear is there? I would have to add a line in my script, that when it is ran, it will delete that post, if it is older than seven days.
Also, is there a way perl can see what date the file was created in? I'm not sure how I should go about determining how old a file is, by days.
Basicly, I would like your input, what would you do in this situation?
Thanks monks

Replies are listed 'Best First'.
Re: File expiration date
by Hofmator (Curate) on Jun 07, 2001 at 20:18 UTC

    Take a look at the file test operators to find out about the age of a file. Furthermore you will have to do the removal of the files by hand/perl yourself ...
    -- Hofmator

Re: File expiration date
by bwana147 (Pilgrim) on Jun 07, 2001 at 20:17 UTC

    Sorry it doesn't use Perl, but you could put something like this in your crontab

    0 * * * * find yourdir -mtime +7 | xargs rm

    Every hour, it deletes all files in "yourdir" that have last been modified more than seven days ago.

    --bwana147

Re: File expiration date
by petdance (Parson) on Jun 07, 2001 at 20:19 UTC
    If you want to find the age of the file, look into the file test operators -M, -A and -C.

    xoxo,
    Andy

    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #   http://petdance.com
    "hack";print map delete$_{$_},split//,q<   andy@petdance.com   >
    
Re: File expiration date
by Beatnik (Parson) on Jun 07, 2001 at 20:24 UTC
    You probably need a cron job (available on any flavor of UN*X; Host restrictions not included) to run a check on weither or not posts are 7 (or more) days old. It all depends on how you'll be implementing it. I'd recommend a SQL backend (DBI would be nice). If you're gonna use plain files, stat is that nifty file stats function (and yeah -X has -M). if you want to play it safe, you can just ignore old(er) posts and only show newer ones (as in don't delete older posts).

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.
Re: File expiration date
by Melly (Chaplain) on Jun 07, 2001 at 20:18 UTC
    Well, there's nothing to stop you doing this in Perl (read up on 'stat'), but a cron might be simpler (using 'find').
    place something like:
    find '/usr/home/foobar/' -name '*.add' -mtime +7 -exec rm {} \;
    in your crontab.
    BTW I don't use *nix much, so don't take this as gospel, more of a very IMHO.
    Tom Melly, tom@tomandlu.co.uk
Re: File expiration date
by bikeNomad (Priest) on Jun 07, 2001 at 20:39 UTC
    Something like:

    my $filename = 'abc.def.ghi'; # delete file if too old if (-M $filename > 7) { unlink($filename) } else { # do something with file }
Re: File expiration date
by andreychek (Parson) on Jun 07, 2001 at 20:22 UTC
    Well, I'm wondering if it might be easier for you to use
    a database, as opposed to files. What you could for every
    classified ad is have a field for (amongst other things)
    the ad itself, and the day it was posted. Whenever it is
    posted, just use Perl to generate a datestamp and put it
    into that database field. Then every evening, you could
    have a cron job to dig through the database and expire (delete?)
    entries that are 7 days old.

    To use a database this way, you wouldn't even be required to
    have a full fledged database server running in the background,
    you could use something as simple as CSV files if thats what
    you want. OTOH, MySQL and Postgres come pretty cheap :-)
    -Eric