File Coverage

blib/lib/String/PerlIdentifier.pm
Criterion Covered Total %
statement 44 44 100.0
branch 33 34 97.0
condition 8 9 88.8
subroutine 5 5 100.0
pod 0 1 0.0
total 90 93 96.7


line stmt bran cond sub pod time code
1             package String::PerlIdentifier;
2 6     6   243424 use 5.006001;
  6         25  
  6         407  
3 6     6   35 use strict;
  6         15  
  6         243  
4 6     6   32 use base qw(Exporter);
  6         15  
  6         1109  
5             our @EXPORT = qw{ make_varname };
6             our $VERSION = "0.05";
7 6     6   33 use Carp;
  6         10  
  6         5931  
8              
9             our @lower = qw(a b c d e f g h i j k l m n o p q r s t u v w x y z);
10             our @upper = map { uc($_) } @lower;
11             #our @eligibles = (@upper, @lower, q{_});
12             #our @chars = (@eligibles, 0..9);
13             our @alphas = (@upper, @lower);
14              
15             our %forbidden = ();
16             our $MIN = 3;
17             our $MAX = 20;
18             our $DEFAULT = 10;
19              
20             sub make_varname {
21 103     103 0 211876 my $length;
22 103         163 my (@eligibles, @chars);
23 103         145 my $scoresflag = 1;
24 103 100 100     709 if (defined $_[0] and ref($_[0]) eq 'HASH') {
25 75         119 my $argsref = shift;
26 75 100 66     360 if ( (defined $argsref->{underscores}) &&
27             ($argsref->{underscores} == 0) ) {
28 10         16 $scoresflag = 0;
29             }
30 75 100       214 if (defined $argsref->{min}) {
31 14 100       336 croak "Minimum must be all numerals: $!"
32             unless $argsref->{min} =~ /^\d+$/;
33 13 100       445 croak "Cannot set minimum length less than 2: $!"
34             unless $argsref->{min} >= 2;
35 12         28 $MIN = $argsref->{min};
36             }
37 73 100       429 if (defined $argsref->{max}) {
38 34 100       1696 croak "Maximum must be all numerals: $!"
39             unless $argsref->{max} =~ /^\d+$/;
40 33 100       232 croak "Cannot set maximum length less than 3: $!"
41             unless $argsref->{max} >= 3;
42 32         61 $MAX = $argsref->{max};
43             }
44 71 100       213 if (defined $argsref->{default}) {
45 24 100       386 croak "Default must be all numerals: $!"
46             unless $argsref->{default} =~ /^\d+$/;
47 23         48 $DEFAULT = $argsref->{default};
48             }
49 70 100 100     246 if ( (defined $argsref->{min}) &&
50             (defined $argsref->{max}) ) {
51 4 100       1215 croak "Minimum must be <= Maximum: $!"
52             if $argsref->{min} >= $argsref->{max};
53             }
54             } else {
55 28         54 $length = shift;
56             }
57 96 100       240 $length = $DEFAULT if ! defined $length;
58 96 100       281 $length = $MIN if $length < $MIN;
59 96 100       223 $length = $MAX if $length > $MAX;
60 96 100       1138 @eligibles = $scoresflag ? (@alphas, q{_}) : (@alphas) ;
61 96         1475 @chars = (@eligibles, 0..9);
62            
63 96         142 my $varname;
64 96         716 MKVAR: {
65 96         115 $varname = $eligibles[int(rand(@eligibles))];
66 96         952 $varname .= $chars[int(rand(@chars))] for (1 .. ($length - 1));
67 96 50       280 next MKVAR if $forbidden{$varname};
68             }
69 96         1784 return $varname;
70             }
71              
72             #################### DOCUMENTATION ###################
73              
74             =head1 NAME
75              
76             String::PerlIdentifier - Generate a random name for a Perl variable
77              
78             =head1 VERSION
79              
80             This document refers to version 0.05, released April 21, 2004.
81              
82             =head1 SYNOPSIS
83              
84             use String::PerlIdentifier;
85              
86             $varname = make_varname(); # defaults to 10 characters
87              
88             or
89              
90             $varname = make_varname(12); # min: 3 max: 20
91              
92             or
93              
94             $varname = make_varname( { # set your own attributes
95             min => $minimum,
96             max => $maximum,
97             default => $default,
98             } );
99              
100             or
101              
102             $varname = make_varname( { # no underscores in strings;
103             underscores => 0, # alphanumerics only
104             } );
105              
106             =head1 DESCRIPTION
107              
108             This module automatically exports a single subroutine, C,
109             which returns a string composed of random characters that qualifies as
110             the name for a Perl variable. The characters are limited to upper- and
111             lower-case letters in the English alphabet, the numerals from 0 through 9
112             and the underscore character. The first character may not be a numeral.
113              
114             By default, C returns a string of 10 characters, but if a
115             numerical argument between 3 and 20 is passed to it, a string of that length
116             will be returned. Arguments smaller than 3 are rounded up to 3; arguments
117             greater than 20 are rounded down to 20.
118              
119             C can also take as an argument a reference to a hash
120             containing one or more of the following keys:
121              
122             min
123             max
124             default
125             underscores
126              
127             So if you wanted your string to contain a minimum of 15 characters and a
128             maximum of 30, you would call:
129              
130             $varname = make_varname( { min => 15, max => 30 } );
131              
132             If you try to set C greater than C, you will get an error message
133             and C. But if you set C less than C or greater than
134             C, the default value will be raised to the minimum or lowered to the
135             maximum as is appropriate.
136              
137             =head2 No underscores option
138              
139             The only meaningful value for key C is C<0>.
140             String::PerlIdentifier, like Perl itself, assumes that underscores are valid
141             parts of identifiers, so underscores are ''on'' by default. So the only time
142             you need to worry about the C element in the hash passed by
143             reference to C is when you want to I underscores from
144             being part of the string being generated -- in which case you set:
145              
146             underscores => 0
147              
148             =head2 Non-Perl-identifier usages
149              
150             Although the strings returned by C qualify as Perl
151             identifiers, they also are a subset of the set of valid directory and file
152             names on operating systems such as Unix and Windows. This is how, for
153             instance, this module's author uses C.
154              
155             B String::PerlIdentifier originally appeared on CPAN as
156             String::MkVarName. When I went to register it on F,
157             C persuaded me to change the name.
158              
159             =head1 TO DO
160              
161             Ideally, you should be able to pass the function a list of strings
162             forbidden to be returned by C, I a list of all
163             Perl variables currently in scope. String::PerlIdentifier doesn't do that yet.
164              
165             =head1 SEE ALSO
166              
167             =over 4
168              
169             =item String::MkPasswd
170              
171             This CPAN module by Chris Grau was the inspiration for String::PerlIdentifier.
172             String::PerlIdentifier evolved as a simplification of String::MkPasswd for
173             use in the test suite for my other CPAN module File::Save::Home.
174              
175             =item String::Random
176              
177             This CPAN module by Steven Pritchard is a more general solution to the problem
178             of generating strings composed of random characters. To generate a
179             10-character string that would qualify as a Perl identifier using
180             String::Random, you would proceed as follows:
181              
182             use String::Random;
183             $rr = String::Random->new();
184             $rr->{'E'} = [ 'A'..'Z', 'a'..'z', '_' ];
185             $rr->{'F'} = [ 'A'..'Z', 'a'..'z', '_', 0..9 ];
186              
187             then
188              
189             $rr->randpattern("EFFFFFFFFF");
190              
191             String::Random's greater generality comes at the cost of more typing.
192              
193             =item File::Save::Home
194              
195             CPAN module by the same author as String::PerlIdentifier which uses
196             C in its test suite as of its version 0.05.
197             File::Save::Home is used internally within recent versions of
198             ExtUtils::ModuleMaker and its test suite.
199              
200             =back
201              
202             =head1 AUTHOR
203              
204             James E Keenan
205             CPAN ID: JKEENAN
206             jkeenan@cpan.org
207             http://search.cpan.org/~jkeenan
208              
209             =head1 SUPPORT
210              
211             Send email to jkeenan [at] cpan [dot] org. Please include any of the
212             following in the subject line:
213              
214             String::PerlIdentifier
215             String-PerlIdentifier
216             make_varname
217              
218             in the subject line. Please report any bugs or feature requests to
219             C, or through the web interface at
220             L.
221              
222             =head1 COPYRIGHT
223              
224             This program is free software; you can redistribute
225             it and/or modify it under the same terms as Perl itself.
226              
227             The full text of the license can be found in the
228             LICENSE file included with this module.
229              
230             =cut
231              
232             1;
233              
234