in reply to Help in building Regular expression
You don't want system('uuidgen') here. system executes a command, but doesn't return the output of the command (it returns the command's exit status - the equivalent of $? in shell scripts). You want backticks.
A fairly direct translation would be:
my $string = `uuidgen`; $string =~ tr [\-abcdef] [9012345]; my $first_10 = substr($string, 0, 10);
The tr operator (is it technically an operator? it's documented in perlop) acts much like the 'tr' command in many shells. substr extracts a substring from a string. The first argument is the string to extract from; the second argument is the character to start at (in this example, 0, so start at the beginning of the string) and the third is the number of characters to take (10 here). The third argument can be omitted, in which case substr extracts to the end of the string.
A slightly better way though might be to avoid spawning the external program and use Data::UUID (or a similar module like UUID::Tiny).
use Data::UUID; my $string = Data::UUID->new->create_str; $string =~ tr [\-abcdef] [9012345]; my $first_10 = substr($string, 0, 10);
use UUID::Tiny; my $string = create_UUID(); $string =~ tr [\-abcdef] [9012345]; my $first_10 = substr($string, 0, 10);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Help in building Regular expression
by begin_101 (Initiate) on Jan 17, 2012 at 23:11 UTC | |
|
Re^2: Help in building Regular expression
by Anonymous Monk on Jan 18, 2012 at 03:45 UTC |