0

I'm new to Perl, and I need to generate a unique ID with 6 characters and only numbers, is there a way to do that simply?

I found :

use UUID::Generator::PurePerl;

sub create_search_id {
    my $this =shift;
    my $args=shift;
    my $ug = UUID::Generator::PurePerl->new();
    my $uuid1 = $ug->generate_v1();
    return $uuid1;
}

But the use statement generates an internal server error... Thanks !

TobyLL
  • 2,098
  • 2
  • 17
  • 23
Clément Andraud
  • 9,103
  • 25
  • 80
  • 158
  • 1
    What internal server error does it generate? You'll need to look in your log files because the specifics of internal server errors are not reported to clients. – Quentin Jun 09 '15 at 12:22
  • http://perldoc.perl.org/perlfaq1.html#What's-the-difference-between-%22perl%22-and-%22Perl%22%3f – Quentin Jun 09 '15 at 12:22
  • 3
    You have installed the module `UUID::Generator::PurePerl`? – Jens Jun 09 '15 at 12:25
  • possible duplicate of [How we can create a Unique Id in Perl](http://stackoverflow.com/questions/18628244/how-we-can-create-a-unique-id-in-perl) – serenesat Jun 09 '15 at 12:43
  • Does the error start with this: `Can't locate UUID/Generator/PurePerl.pm in @INC`? If so, do this: `perl -MCPAN -e 'install UUID::Generator::PurePerl'` at the command line. If not, and "Internal server error" is literally your error, we need more info, such as whether this is a web GUI, along with any error log entries surrounding the running of the code. – stevieb Jun 09 '15 at 13:11
  • By `6 characters` do you mean `0` through `9` and `A` through `F` (i.e. hexadecimal) or do you mean only six digits? If the latter (i.e. 6 digits), you only have a million unique numbers: I'd just start with `000000` then `000001` then ... until `999999` (which is where you have to stop) and if necessary save the number to a file and read it back in when you need the next one. – kjpires Jun 09 '15 at 15:28

1 Answers1

2

A UUID is a special form of unique identifier; any UUID-generating module is unlikely to support creating an identifier in the form you want.

I'm not certain what "6 characters and only numbers" means; if you mean 6 digits, just something like this:

my $id = join '', map int rand 10, 1..6;

But to make it unique, you'd need to be able to check that it isn't already in use, and you haven't told us anything about how you are using it.

ysth
  • 96,171
  • 6
  • 121
  • 214