|
NAMEString::Random - Perl module to generate random strings based on a patternVERSIONversion 0.32SYNOPSISuse String::Random; my $string_gen = String::Random->new; print $string_gen->randregex('\d\d\d'); # Prints 3 random digits # Prints 3 random printable characters print $string_gen->randpattern("..."); or use String::Random qw(random_regex random_string); print random_regex('\d\d\d'); # Also prints 3 random digits print random_string("..."); # Also prints 3 random printable characters DESCRIPTIONThis module makes it trivial to generate random strings.As an example, let's say you are writing a script that needs to generate a random password for a user. The relevant code might look something like this: use String::Random; my $pass = String::Random->new; print "Your password is ", $pass->randpattern("CCcc!ccn"), "\n"; This would output something like this: Your password is UDwp$tj5 NOTE!!!: currently, "String::Random" defaults to Perl's built-in predictable random number generator so the passwords generated by it are insecure. See the "rand_gen" option to "String::Random" constructor to specify a more secure random number generator. There is no equivalent to this in the procedural interface, you must use the object-oriented interface to get this functionality. If you are more comfortable dealing with regular expressions, the following code would have a similar result: use String::Random; my $pass = String::Random->new; print "Your password is ", $pass->randregex('[A-Z]{2}[a-z]{2}.[a-z]{2}\d'), "\n"; PatternsThe pre-defined patterns (for use with "randpattern()" and "random_pattern()") are as follows:c Any Latin lowercase character [a-z] C Any Latin uppercase character [A-Z] n Any digit [0-9] ! A punctuation character [~`!@$%^&*()-_+={}[]|\:;"'.<>?/#,] . Any of the above s A "salt" character [A-Za-z0-9./] b Any binary data These can be modified, but if you need a different pattern it is better to create another pattern, possibly using one of the pre-defined as a base. For example, if you wanted a pattern "A" that contained all upper and lower case letters ("[A-Za-z]"), the following would work: my $gen = String::Random->new; $gen->{'A'} = [ 'A'..'Z', 'a'..'z' ]; or my $gen = String::Random->new; $gen->{'A'} = [ @{$gen->{'C'}}, @{$gen->{'c'}} ]; or my $gen = String::Random->new; $gen->set_pattern(A => [ 'A'..'Z', 'a'..'z' ]); The random_string function, described below, has an alternative interface for adding patterns. Methods
Functions
BUGSThis is Bug FreeX code. (At least until somebody finds oneX)Please report bugs here: <https://rt.cpan.org/Public/Dist/Display.html?Name=String-Random> . AUTHOROriginal Author: Steven Pritchard "steve@silug.org"Now maintained by: Shlomi Fish ( <http://www.shlomifish.org/> ). LICENSEThis program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.SEE ALSOperl(1).SUPPORTWebsitesThe following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
Bugs / Feature RequestsPlease report any bugs or feature requests by email to "bug-string-random at rt.cpan.org", or through the web interface at <https://rt.cpan.org/Public/Bug/Report.html?Queue=String-Random>. You will be automatically notified of any progress on the request by the system.Source CodeThe code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :)<https://github.com/shlomif/string-random> git clone http://github.com/shlomif/String-Random AUTHORShlomi Fish <shlomif@cpan.org>BUGSPlease report any bugs or feature requests on the bugtracker website <https://github.com/shlomif/string-random/issues>When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. COPYRIGHT AND LICENSEThis software is copyright (c) 2021 by Shlomi Fish.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Visit the GSP FreeBSD Man Page Interface. |