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

We are using Perl 5.005_03 and cannot upgrade because our client base has not.

I need to basically keep some information available and modifiable across invocations of a Perl script and a tied Perl hash seems ideal. However:

I only want the information for the hash to exist for that day. Meaning, when the script is run the next day, its contents are initially undefined and then as it is used its contents accumulate over the day.

One thought I had is to force the hash-tying module to tie its files to a directory, whose name is today's date. The other thought I had was to make the first 3 fields of the hash the day,month, and year. The final thought I hash was to use Tie::CacheHash and define the sort order to be the date.

Replies are listed 'Best First'.
Re: A Tied Dated Hash?
by lhoward (Vicar) on Oct 18, 2000 at 21:28 UTC
    Why not just use the timestamp on the tied hash file before you open it. Then if the hash file timestamp isn't the current date then empty the hash first thing after tieing to it.
    • What module should I use for this purpose?
    • How do you get the timestamp of a file?

        UPDATE In the interests of having a workable skeleton that takes account of Fastolfe's point below about -M not being time-compatible, this code has been modified from its original form

        You could use the builtin -M test, to find out the last time the file was modified (see Fastolfe's comment below for why this might not be the best way to get the time!).

        Here's something skeletal you could build on.

        my @today = (localtime)[3..5]; # gets day, month-1, and year my $last_modified = (stat($tied_hash_file))[9]; # grab the modificatio +n time my @last_modified_date = (localtime($last_modified))[3..5]; # search perlfaq4 (I think) for the compare_array routine! # ... tie the hash, then %tied_hash = () unless compare_array(\@today, \@last_modified_date);

        Philosophy can be made out of anything -- or less

        I don't know what the best module to use here, but stat will give you information about files. See the perlfunc manpage.
Re: A Tied Dated Hash?
by little (Curate) on Oct 18, 2000 at 21:18 UTC
    Why don't u use a server log like syntax, so you always acces a file with the same name, but when you pass 24.00 o'clock, you simply rename the file with the date and the month in the name and open a new one with the standard name ??
    Have a nice day
    All decision is left to your taste
Re: A Tied Dated Hash?
by AgentM (Curate) on Oct 19, 2000 at 01:49 UTC
    You could bind the script to a cron job which restarts every 24 hours. If your script is just blocking on a pipe, you can keep it on all the time. At the end of the day, the script will be reset and all relevant info reset as well. I'm not exactly sure why you need to tie your hash to a file, but if you say so....

    If your script if invocated by other scripts multiple times a day, you may encounter a speed issue. Running the program consistently may be the faster option.

    If you insist on using the tie, then use the cron job idea but unlink the tied file as soon as its created. It will not be deleted unless your program quits which will be on error or on 24-hour rotations.

    AgentM Systems nor Nasca Enterprises nor Bone::Easy is responsible for the comments made by AgentM.