Friday, February 8, 2013

Generate a random string

takes one argument: length of string (default = 32)
#! /usr/bin/perl -w

use strict;
use warnings;

my $length = shift || 32;
my $customchars = shift;

my @chars;
@chars = split(//, $customchars) if defined($customchars);
@chars = ('a'..'z','A'..'Z','0'..'9','_') unless defined($customchars);

my $string;
foreach (1..$length) {
$string .= $chars[rand @chars];
}

print $string."\n";

 

No comments:

Post a Comment