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

DBD::CSV is the perfect tool for a small project I am working on right now. The only thing missing is auto generation of a unique key for each row.

I have thought about building my own "autonumber" routine by saving the last number used in a config file, and incrementing it each time I add a record. Am I missing some obvious pitfalls in doing this? Is this the best way to go, or are there better ways of generating a unique key?

With humble thanks,
digger

Replies are listed 'Best First'.
Re: DBD::CSV and unique keys
by waswas-fng (Curate) on Nov 14, 2002 at 16:09 UTC
    I like to use Time::HiRes time() and s/.// to get a long unique number. You dont have to worry about file locking (to prevent clobbering if multiple instances try to get a number at the same time).

    -Waswas
Re: DBD::CSV and unique keys
by Tanalis (Curate) on Nov 14, 2002 at 16:14 UTC
    You could always query the table to find out how many rows it has, then start to insert with $numRows + 1 as your first unique key, incrementing this variable for each row thereafter.

    That'd work as long as you're not threading, or running multiple instances of the script at the same time.

    Hope that helps ..
    --Foxcub

      carefull with this.... if you start deleting rows, ($numRows + 1) wont guarantee uniqueness.

      if you choose to do the config file way- take a look around here for some good file locking examples.

      here's the file locking tutorial.