Gary Yang wrote:
> Hi,
>  
> I need to get a random number whenever the perl script is called. Each 
> time the random number I got should be different.  I use that number to 
> name generated files, i.e. I want the perl script to generate different 
> file names whenever it is called. Can someone tell me how to get the 
> different random number whenever the perl script is called?

A different random number, that is a contradiction:-)

You could start with rand() [ perldoc -f rand ], but you would
still have to check to see if the file existed!

Anothor method is [ perldoc -q temporary ], guides you to File::Temp
but this is mostly intended for temporary files that (in most cases) disappear
when closed.

Depending on you needs, create a filename that is a combination of

        hostname
        clientname
        $^T - secs since epoch  # sorts nicely
        sprintf("%4.4d%2.2d%2.2dT%2.2d%2.2d%2.2d", 1900+$t[5], 
$t[4]+1,@t[3,2,1,0]);
                where @t = gmtime($^T); # or localtime($^T); # both variants 
sorts nicely
        $$ - PID
        rand() - a random number <1 

Andn if you need to be _really_ sure:

>From  perlopentut :

     And here are things you can do with "sysopen" that you
     cannot do with a regular "open".  As you'll see, it's just a
     matter of controlling the flags in the third argument.

     To open a file for writing, creating a new file which must
     not previously exist:

         sysopen(FH, $path, O_WRONLY | O_EXCL | O_CREAT);

Leading to something like:

        # construct a nice meaningful filename in $path, maybe using the some 
of the above methods...
        use IO::File;
        sysopen(FH, $path , O_WRONLY|O_EXCL|O_CREAT) or die "$0: $path: $!";
        print FH "ok\n";


        where you actually may change the "or die" to a loop:
        $path="foo00";
        until( sysopen(FH, $path, O_WRONLY|O_EXCL|O_CREAT) ) { $path++; }; 
print "Got: $path\n";
        But - you end up in a longer and longer loop, and end in trouble if you 
can't create the file
        for some other reason.

        use strict;
        use warnings;
        use Sys::Hostname;
        use IO::File;

        my $dir="/var/tmp";
        my @ts = gmtime(time()); 
        my $prefix = sprintf("%s-%4.4d%2.2d%2.2dT%2.2d.%2.2d-%d",
                                Sys::Hostname::hostname,
                                1900+$ts[5], $ts[4]+1, @ts[3,2,1,0],
                                $$,
                             );
        my $suffix = 'txt';
        my $maxtry=10;
        my $success=0;
        my $i = 0;
        while ( $i < $maxtry ) {        
          my $path = sprintf("%s/%s-%d.%s", $dir, $prefix, $i, $suffix);
          if ( sysopen(FH, $path , O_WRONLY|O_EXCL|O_CREAT)) {
            $success=1;
            last;
          } else {
            die "$0: $path: $!" unless $! =~ /file exists/i;
            $i++;
          }
        }
        die "$0: failed to create unique file - tried $i times" unless $success;

                
        
> I greatly appreciate your help.
>  
>  
> Gary
>  
>  
> 
> ------------------------------------------------------------------------
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try 
> it now. 
> <http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
>  
>  >
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Perl-Unix-Users mailing list
> Perl-Unix-Users@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


-- 
print $Std{'Disclaimer'};
$CC =  +-1; # the Computer Constant - very useful when writing loops.    #
$|=printf "Just another [lazy] %s hacker\r",("PERL","perl")[$i+sleep 1]   #
while $i=$i?0:$CC,1;                                                    ###
__EOF__

_______________________________________________
Perl-Unix-Users mailing list
Perl-Unix-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to