1String::Random(3) User Contributed Perl Documentation String::Random(3)
2
3
4
6 String::Random - Perl module to generate random strings based on a
7 pattern
8
10 use String::Random;
11 $foo = new String::Random;
12 print $foo->randregex('\d\d\d'); # Prints 3 random digits
13 print $foo->randpattern("..."); # Prints 3 random printable characters
14
15 or
16
17 use String::Random qw(random_regex random_string);
18 print random_regex('\d\d\d'); # Also prints 3 random digits
19 print random_string("..."); # Also prints 3 random printable characters
20
22 This module makes it trivial to generate random strings.
23
24 As an example, let's say you are writing a script that needs to
25 generate a random password for a user. The relevant code might look
26 something like this:
27
28 use String::Random;
29 $pass = new String::Random;
30 print "Your password is ", $pass->randpattern("CCcc!ccn"), "\n";
31
32 This would output something like this:
33
34 Your password is UDwp$tj5
35
36 If you are more comfortable dealing with regular expressions, the
37 following code would have a similar result:
38
39 use String::Random;
40 $pass = new String::Random;
41 print "Your password is ",
42 $pass->randregex('[A-Z]{2}[a-z]{2}.[a-z]{2}\d'), "\n";
43
44 Patterns
45 The pre-defined patterns (for use with "randpattern()" and
46 "random_pattern()") are as follows:
47
48 c Any lowercase character [a-z]
49 C Any uppercase character [A-Z]
50 n Any digit [0-9]
51 ! A punctuation character [~`!@$%^&*()-_+={}[]|\:;"'.<>?/#,]
52 . Any of the above
53 s A "salt" character [A-Za-z0-9./]
54 b Any binary data
55
56 These can be modified, but if you need a different pattern it is better
57 to create another pattern, possibly using one of the pre-defined as a
58 base. For example, if you wanted a pattern "A" that contained all
59 upper and lower case letters ("[A-Za-z]"), the following would work:
60
61 $foo = new String::Random;
62 $foo->{'A'} = [ 'A'..'Z', 'a'..'z' ];
63
64 or
65
66 $foo = new String::Random;
67 $foo->{'A'} = [ @{$foo->{'C'}}, @{$foo->{'c'}} ];
68
69 The random_string function, described below, has an alternative
70 interface for adding patterns.
71
72 Methods
73 new
74 new max => number
75 Create a new String::Random object.
76
77 Optionally a parameter "max" can be included to specify the
78 maximum number of characters to return for "*" and other
79 regular expression patters that don't return a fixed number of
80 characters.
81
82 randpattern LIST
83 The randpattern method returns a random string based on the
84 concatenation of all the pattern strings in the list.
85
86 It will return a list of random strings corresponding to the
87 pattern strings when used in list context.
88
89 randregex LIST
90 The randregex method returns a random string that will match
91 the regular expression passed in the list argument.
92
93 Please note that the arguments to randregex are not real
94 regular expressions. Only a small subset of regular expression
95 syntax is actually supported. So far, the following regular
96 expression elements are supported:
97
98 \w Alphanumeric + "_".
99 \d Digits.
100 \W Printable characters other than those in \w.
101 \D Printable characters other than those in \d.
102 . Printable characters.
103 [] Character classes.
104 {} Repetition.
105 * Same as {0,}.
106 ? Same as {0,1}.
107 + Same as {1,}.
108
109 Regular expression support is still somewhat incomplete.
110 Currently special characters inside [] are not supported (with
111 the exception of "-" to denote ranges of characters). The
112 parser doesn't care for spaces in the "regular expression"
113 either.
114
115 Functions
116 random_string PATTERN,LIST
117 random_string PATTERN
118 When called with a single scalar argument, random_string
119 returns a random string using that scalar as a pattern.
120 Optionally, references to lists containing other patterns can
121 be passed to the function. Those lists will be used for 0
122 through 9 in the pattern (meaning the maximum number of lists
123 that can be passed is 10). For example, the following code:
124
125 print random_string("0101",
126 ["a", "b", "c"],
127 ["d", "e", "f"]), "\n";
128
129 would print something like this:
130
131 cebd
132
134 This is Bug Free(TM) code. (At least until somebody finds one...)
135
137 Steven Pritchard <steve@silug.org>
138
140 perl(1).
141
142
143
144perl v5.12.0 2010-05-06 String::Random(3)