In my post I've missed the 'd' token in "%05d"

Here are a few possible solutions that will do all the work for you

Apache/UUID.pm
--------------
package Apache::UUID;
use strict;
my($base, $seq);

die "Cannot push handlers" unless Apache->can('push_handlers');
init();

sub init {
     Apache->push_handlers(
         PerlChildInitHandler => sub {
             $seq  = 0;
             $base = $^T . sprintf("%05d", $$);
             1;
         });
}
sub id { $base . $seq++; }
#sub format { ... }
1;
__END__

startup.pl
----------
use Apache::UUID; # must be loaded at the startup!

test.pl
--------
use Apache::UUID;
print "Content-type: text/plain\n\n";
print Apache::UUID::id();

Since I've used $^T token, the module must be loaded at the startup, or 
someone may modify $^T. If you use time(), you don't need the child init 
handler (but you pay the overhead). and can simply have:

package Apache::UUID;
use strict;
my($base, $seq);

sub id {
    $base ||= time . sprintf("%05d", $$);
    $base . $seq++;
}
1;

the nice thing about the childinit handler is that at run time you just 
need to "$base . $seq++;". but probably this second version is just fine.

also you probably want to add a format() function so you get the ids of 
the same width.

another improvement is to use pack(), which will handle sprintf for you 
and will create a number which is more compact and always of the same width:

package Apache::UUID;
use strict;
my $seq;
sub id { unpack "H*", pack "Nnn", time, $$, $seq++;}
1;

Another problem that you may need to tackle is predictability... but 
that's a different story.

__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:[EMAIL PROTECTED] http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com

Reply via email to