Howto create a unique identifier in perl
published in September 2003
It's probably because I didn't look in the righ places but I just couldn't find a simple way to create a unique identifier in perl. I decided it's not worth the trouble going through the perl man pages (always a thankless task) when it's so easy to create a unique id with a few lines of code.
And here it is
#!/usr/bin/perl
$sessionId ="";
$length=16;
for($i=0 ; $i< $length ;)
{
$j = chr(int(rand(127)));
if($j =~ /[a-zA-Z0-9]/)
{
$sessionId .=$j;
$i++;
}
}
print $sessionId;
It's that simple. You can change the combination of letters and number used in the sessionId by changing this regular expression /[a-zA-Z0-9]/. And change it's length with the $length variable. The longer the identifier the more likely that it's unique.
