title: Generate unique random strings
body: <p>I am writing a very small URL shortener with Dancer. It uses the REST plugin to store a posted URL in a database with a six character string which is used by the user to access the shorted URL.</p>
  
  <p>Now I am a bit unsure about my random string generation method.</p>
  
  <pre><code>sub generate_random_string{
      my $length_of_randomstring = shift; # the length of 
                                          # the random string to generate
  
      my @chars=('a'..'z','A'..'Z','0'..'9','_');
      my $random_string;
      for(1..$length_of_randomstring){
          # rand @chars will generate a random 
          # number between 0 and scalar @chars
          $random_string.=$chars[rand @chars];
      }
  
      # Start over if the string is already in the Database
      generate_random_string(6) if database-&gt;quick_select('urls', { shortcut =&gt; $random_string });
  
      return $random_string;
  }
  </code></pre>
  
  <p>This generates a six char string and calls the function recursively if the generated string is already in the DB. I know there are 63^6 possible strings but this will take some time if the database gathers more entries. And maybe it will become a nearly infinite recursion, which I want to prevent. </p>
  
  <p>Are there ways to generate unique random strings, which prevent recursion?</p>
  
  <p>Thanks in advance</p>