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

we have this Perl forms script (perl 4) that needs to be modified so that it includes a unique character string (number and/or text) automatically inserted into the generated output when the form is submitted with user information.

Currently, the relevant code outputs like this:

 print $ITEMOUT "Subject:   Request_Number:  \n";

with the "Reqest_Number:" followed by blank text, but the new version of our processing software requires that the "Request_Number:" string be followed by a unique number or text string (for the form submission to be processed without errors.)

I'm thinking of getting the date/time from system and inserting it after the "Request_Number:" as a means of generating a unique string.

Any suggestions for doing this using date/time stamp or other better means of generating unique string. This is not a high volume form, so I thought the date/time is "unique enough"

thanks in advance..from someone new to PERL

Replies are listed 'Best First'.
Re: generating unique string
by andreychek (Parson) on Sep 19, 2001 at 20:06 UTC
    Well, if by any chance you are using Apache as your webserver, you could always use the Apache::Session module, which will happily generate unique numbers for you.

    If you choose to make use of Apache::Session for this purpose, you would always have the option down the road to make use of the session support it provides -- allowing you to store data in the server across requests. Good luck!
      The unique ID generation in Apache::Session is actually not very good. It can have collisions, especially when used in a cluster of servers. You should replace that function with something better (I like mod_unique_id) if you use it in a cluster and are concerned about collisions.
      Thanks -- but unfortunately the server is NT 4 (yuk)

        Shouldn't be a problem. Apache runs just fine on NT4.


        "The first rule of Perl club is you don't talk about Perl club."

        Hmmm.. are you saying then that you are using IIS? Some folks still choose to use Apache on Windows NT.

        If you are using IIS, I know if provides unique ID's to all ASP scripts -- it's possible it does the same thing for Perl.. although I couldn't tell you how to determine that info. But perhaps that'll give you a start. It may even be provided to you in an environment variable or with the request string.
        NP: Apache::Session, much like 'Monster Island', is just a name, and can probably be used on whatever webserver you have at hand.


        Hands up those who got the Simpsons reference <g>

Re: gernerating unique string
by dga (Hermit) on Sep 19, 2001 at 20:20 UTC

    Even if its a 'low volume' form you still may get 2 people hitting it at the same time even if you only get 3 hits on a given day.

    One easy one if you aren't worried about people making these up to circumvent the reason you are using a random value in the first place would be to use pid and Time::HiRes;

    use strict; use Time::HiRes qw(time); my $uniq_value="$$" . time;

    $uniq_value will be something like 153331000916182.10074

    This will be unique since the same CGI can't do 2 of these without that last digit stepping with current CPU and network speeds and each process has its own pid so even 2 at the same time will be unique.

    The number could be predicted in advance since its totally linear and not random at all.

Re: gernerating unique string
by broquaint (Abbot) on Sep 19, 2001 at 20:18 UTC
    You could use some high resolution time -
    use Time::HiRes qw(gettimeofday); $id = gettimeofday();
    or if you've got time on your hands -
    use Digest::MD5 qw(md5_hex); use Time::HiRes qw(gettimeofday); $id = md5_hex(gettimeofday());
    Although you'll may have to download them from CPAN (here's Digest::MD5 and Time::HiRes)


Re: unique string (Russ: security concerns)
by Russ (Deacon) on Sep 19, 2001 at 22:58 UTC
    I haven't seen any discussion about security, so this may not apply; however, if you intend for this unique string to keep other users from "hijacking" some submission... There may be no such thing as "unique enough."

    Here is one of my earlier posts on this topic about randomness and security. Hope it helps.

    Brainbench 'Most Valuable Professional' for Perl

Re: gernerating unique string
by perrin (Chancellor) on Sep 19, 2001 at 22:10 UTC
    You should use a counter file, with basic locking. It's just like a web counter. There are CPAN modules for this.