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);

In reply to Re: Help in building Regular expression by tobyink
in thread Help in building Regular expression by begin_101

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.