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

I'm using DB_File DB_BTREE for an image upload script. I have it setup like $image{localtime()} = "$imageinfo";. I was wondering how localtime really works.

Since I'm using localtime as a key, I was wondering what would happen if I set it up so four files can be uploaded on the same page. Would they overwrite eachother (because they'd all share the same key) or would localtime be different for each image?

I guess the question is, is localtime determined when all the information is passed, or just when that piece of information is passed (where there would be a fraction of a second difference between image processing)?

Thank you fellow monks for your help.

"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

edited: Sun Jul 27 15:24:06 2003 by jeffa - title change (was: localtime [278160])

  • Comment on Using localtime() as a unique identifier

Replies are listed 'Best First'.
(jeffa) Re: Using localtime() as a unique identifier
by jeffa (Bishop) on Jul 27, 2003 at 00:02 UTC
    You should use a key that will allow you to identify each row uniquely. If you insert multiple records into the database "at the same time", chances are some of them will be inserted within the same second. localtime's smallest unit of measurement is a second, so it probably is not going to garuantee uniqueness very well ...
    $ perl -le"print localtime for 0..9" 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061 55161926610362061
    Time::HiRes, however:
    $ perl -MTime::HiRes=time -le"print time for 0..9" 1059265047.22228 1059265047.22246 1059265047.22248 1059265047.22251 1059265047.22253 1059265047.22255 1059265047.22257 1059265047.22259 1059265047.22261 1059265047.22264
    may work very well for your needs. But, keep in mind that a simple integer works well if you remember to keep it incremented. This is one reason why i prefer a database management system instead. (but i wouldn't be surprised if a CPAN module exists that auto increments a key for you.)

    UPDATE: i ran the second Perl one-liner again and redirected to a file. Here's the contents of that file:

    1059265476.27685 1059265476.27695 1059265476.27696 1059265476.27696 1059265476.27697 1059265476.27697 1059265476.27698 1059265476.27698 1059265476.27699 1059265476.27699
    Like i said, an auto incremented id makes uniqueness a lot easier to maintain.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Using localtime() as a unique identifier
by Limbic~Region (Chancellor) on Jul 26, 2003 at 23:56 UTC
    sulfericacid,

    perldoc -f localtime

    You are using localtime in scalar context so you are going to have keys that look like Sat Jul 26 19:52:08 2003. It uses time if no time string is given.

    Since it uses time, there is very likely a chance you will end up generating keys of the same name so you should check the existance before blindly assigning values. You may want to put the assignment in a loop which adds successive suffixes until a unique key exists.

    Hope this helps - L~R

Re: Using localtime() as a unique identifier
by chromatic (Archbishop) on Jul 26, 2003 at 23:59 UTC

    I'm not sure I understand the question. The time is determined when the function is called. In this case, I'm not sure it matters — if your localtime has a one second granularity (and I consider that fairly likely), it's likely that you'll whack the first three images.

    Something like Time::HiRes would be much better in this case.

Re: Using localtime() as a unique identifier
by sauoq (Abbot) on Jul 27, 2003 at 00:27 UTC

    Why are you keying by time at all? Are you really going to retrieve the images from your store using the time they were uploaded? Or are you just trying to generate unique keys? If the latter, I suggest you consider a different method of generating your unique keys with special attention given to how you intend to access them.

    -sauoq
    "My two cents aren't worth a dime.";