From b397c9b78d121926173d0f60b3dd661d5b064db6 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:29:05 +0200 Subject: [PATCH 01/56] Add a test for trailing whitespace. Conflicts: META.yml --- Build.PL | 0 MANIFEST | 4 +++- t/style-trailing-space.t | 30 ++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) mode change 100755 => 100644 Build.PL create mode 100644 t/style-trailing-space.t diff --git a/Build.PL b/Build.PL old mode 100755 new mode 100644 diff --git a/MANIFEST b/MANIFEST index 8401b74..874b648 100644 --- a/MANIFEST +++ b/MANIFEST @@ -2,8 +2,8 @@ Build.PL Changes MANIFEST META.yml -TODO README +TODO lib/String/Random.pm t/01_use.t t/02_new.t @@ -11,3 +11,5 @@ t/03_random_string.t t/04_randpattern.t t/05_randregex.t t/06_random_regex.t +t/style-trailing-space.t +META.json diff --git a/t/style-trailing-space.t b/t/style-trailing-space.t new file mode 100644 index 0000000..131a278 --- /dev/null +++ b/t/style-trailing-space.t @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +eval "use Test::TrailingSpace"; +if ($@) +{ + plan skip_all => "Test::TrailingSpace required for trailing space test."; +} +else +{ + plan tests => 1; +} + +# TODO: add .pod, .PL, the README/Changes/TODO/etc. documents and possibly +# some other stuff. +my $finder = Test::TrailingSpace->new( + { + root => '.', + filename_regex => qr/(?:\.(?:t|pm|pl|xs|c|h|txt|pod|PL)|README|Changes|TODO|LICENSE)\z/, + }, +); + +# TEST +$finder->no_trailing_space( + "No trailing space was found." +); From 984138d3c6b07d958b958916bfced62342bb0eda Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:30:15 +0200 Subject: [PATCH 02/56] Add gitignore. --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c9db390 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +Build +META.json +META.yml +MYMETA.json +MYMETA.yml +String-Random-0* +_build +blib From 4c9f16c56712d2eb7b7870265838fc505ab9a659 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:32:05 +0200 Subject: [PATCH 03/56] Add changes. --- Changes | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Changes b/Changes index 62df5f8..8ba537b 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ Revision history for Perl extension String::Random: +- Add t/style-trailing-space.t . + 0.22 Thu Sep 21 2006 - Fix README to reflect current reality. - Random cleanup in this file. From 12a914f6e3fdc1974725ed27e56336c7788880a2 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:46:03 +0200 Subject: [PATCH 04/56] Update the POD. --- Changes | 2 ++ MANIFEST | 3 ++- lib/String/Random.pm | 63 ++++++++++++++++++++++++++------------------ t/pod.t | 12 +++++++++ 4 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 t/pod.t diff --git a/Changes b/Changes index 8ba537b..f1d7a50 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,8 @@ Revision history for Perl extension String::Random: - Add t/style-trailing-space.t . +- Update the POD in lib/String/Random.pm . +- Add t/pod.t . 0.22 Thu Sep 21 2006 - Fix README to reflect current reality. diff --git a/MANIFEST b/MANIFEST index 874b648..2b40b7f 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,6 +1,7 @@ Build.PL Changes MANIFEST +META.json META.yml README TODO @@ -11,5 +12,5 @@ t/03_random_string.t t/04_randpattern.t t/05_randregex.t t/06_random_regex.t +t/pod.t t/style-trailing-space.t -META.json diff --git a/lib/String/Random.pm b/lib/String/Random.pm index d68bd67..5335a53 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -279,22 +279,25 @@ sub random_string { 1; __END__ +=encoding utf8 + =head1 NAME String::Random - Perl module to generate random strings based on a pattern =head1 SYNOPSIS - use String::Random; - $foo = new String::Random; - print $foo->randregex('\d\d\d'); # Prints 3 random digits - print $foo->randpattern("..."); # Prints 3 random printable characters + use 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("..."); I - 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 + 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 =head1 DESCRIPTION @@ -304,19 +307,22 @@ 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; - $pass = new String::Random; - print "Your password is ", $pass->randpattern("CCcc!ccn"), "\n"; + 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 +B: currently, String::Random uses Perl's built-in predictable random +number generator so the passwords generated by it are insecure. + If you are more comfortable dealing with regular expressions, the following code would have a similar result: use String::Random; - $pass = new String::Random; + my $pass = String::Random->new; print "Your password is ", $pass->randregex('[A-Z]{2}[a-z]{2}.[a-z]{2}\d'), "\n"; @@ -325,8 +331,8 @@ code would have a similar result: The pre-defined patterns (for use with C and C) are as follows: - c Any lowercase character [a-z] - C Any uppercase character [A-Z] + 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 @@ -338,13 +344,13 @@ create another pattern, possibly using one of the pre-defined as a base. For example, if you wanted a pattern C that contained all upper and lower case letters (C<[A-Za-z]>), the following would work: - $foo = new String::Random; - $foo->{'A'} = [ 'A'..'Z', 'a'..'z' ]; + my $gen = String::Random->new; + $gen->{'A'} = [ 'A'..'Z', 'a'..'z' ]; I - $foo = new String::Random; - $foo->{'A'} = [ @{$foo->{'C'}}, @{$foo->{'c'}} ]; + my $gen = String::Random->new; + $gen->{'A'} = [ @{$gen->{'C'}}, @{$gen->{'c'}} ]; The random_string function, described below, has an alternative interface for adding patterns. @@ -360,8 +366,8 @@ for adding patterns. Create a new String::Random object. Optionally a parameter C can be included to specify the maximum number -of characters to return for C<*> and other regular expression patters that -don't return a fixed number of characters. +of characters to return for C<*> and other regular expression patterns that +do not return a fixed number of characters. =item randpattern LIST @@ -413,27 +419,34 @@ containing other patterns can be passed to the function. Those lists will be used for 0 through 9 in the pattern (meaning the maximum number of lists that can be passed is 10). For example, the following code: - print random_string("0101", - ["a", "b", "c"], - ["d", "e", "f"]), "\n"; + print random_string("0101", + ["a", "b", "c"], + ["d", "e", "f"]), "\n"; would print something like this: - cebd + cebd =back =head1 BUGS -This is Bug Free(TM) code. (At least until somebody finds one...) +This is Bug Free™ code. (At least until somebody finds one…) + +Please report bugs here: + +L . =head1 AUTHOR -Steven Pritchard +Original Author: Steven Pritchard C<< steve@silug.org >> + +Now maintained by: Shlomi Fish ( L ). =head1 SEE ALSO perl(1). =cut + # vi: set ai et: diff --git a/t/pod.t b/t/pod.t new file mode 100644 index 0000000..ee8b18a --- /dev/null +++ b/t/pod.t @@ -0,0 +1,12 @@ +#!perl -T + +use strict; +use warnings; +use Test::More; + +# Ensure a recent version of Test::Pod +my $min_tp = 1.22; +eval "use Test::Pod $min_tp"; +plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; + +all_pod_files_ok(); From 58b6f9ebbc68106e917e2a1c07296dc8bf2c58aa Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:52:40 +0200 Subject: [PATCH 05/56] Convert Build.PL to Test::Run::Builder. --- Build.PL | 7 ++-- Changes | 1 + MANIFEST | 1 + inc/Test/Run/Builder.pm | 79 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 inc/Test/Run/Builder.pm diff --git a/Build.PL b/Build.PL index ab62b20..bddb637 100644 --- a/Build.PL +++ b/Build.PL @@ -1,8 +1,11 @@ use strict; use warnings; -use Module::Build; -my $builder = Module::Build->new( +use lib './inc'; + +use Test::Run::Builder; + +my $builder = Test::Run::Builder->new( module_name => 'String::Random', license => 'perl', dist_author => 'Steven Pritchard ', diff --git a/Changes b/Changes index f1d7a50..66a5d4c 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,7 @@ Revision history for Perl extension String::Random: - Add t/style-trailing-space.t . - Update the POD in lib/String/Random.pm . - Add t/pod.t . +- Convert Build.PL to Test::Run::Builder. 0.22 Thu Sep 21 2006 - Fix README to reflect current reality. diff --git a/MANIFEST b/MANIFEST index 2b40b7f..67c14c6 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ META.json META.yml README TODO +inc/Test/Run/Builder.pm lib/String/Random.pm t/01_use.t t/02_new.t diff --git a/inc/Test/Run/Builder.pm b/inc/Test/Run/Builder.pm new file mode 100644 index 0000000..5703d01 --- /dev/null +++ b/inc/Test/Run/Builder.pm @@ -0,0 +1,79 @@ +package Test::Run::Builder; + +use strict; +use warnings; + +use Module::Build; + +use vars qw(@ISA); + +@ISA = (qw(Module::Build)); + +sub ACTION_runtest +{ + my ($self) = @_; + my $p = $self->{properties}; + + $self->depends_on('code'); + + local @INC = @INC; + + # Make sure we test the module in blib/ + unshift @INC, (File::Spec->catdir($p->{base_dir}, $self->blib, 'lib'), + File::Spec->catdir($p->{base_dir}, $self->blib, 'arch')); + + $self->do_test_run_tests; +} + +sub ACTION_distruntest { + my ($self) = @_; + + $self->depends_on('distdir'); + + my $start_dir = $self->cwd; + my $dist_dir = $self->dist_dir; + chdir $dist_dir or die "Cannot chdir to $dist_dir: $!"; + # XXX could be different names for scripts + + $self->run_perl_script('Build.PL') # XXX Should this be run w/ --nouse-rcfile + or die "Error executing 'Build.PL' in dist directory: $!"; + $self->run_perl_script('Build') + or die "Error executing 'Build' in dist directory: $!"; + $self->run_perl_script('Build', [], ['runtest']) + or die "Error executing 'Build test' in dist directory"; + chdir $start_dir; +} + +sub do_test_run_tests +{ + my $self = shift; + + require Test::Run::CmdLine::Iface; + + my $test_run = + Test::Run::CmdLine::Iface->new( + { + 'test_files' => [glob("t/*.t")], + } + # 'backend_params' => $self->_get_backend_params(), + ); + + return $test_run->run(); +} + +sub ACTION_tags +{ + my $self = shift; + return + $self->do_system( + "ctags", + qw(-f tags --recurse --totals + --exclude=blib/** --exclude=t/lib/** + --exclude=**/.svn/** --exclude='*~'), + "--exclude=".$self->dist_name()."-*/**", + qw(--languages=Perl --langmap=Perl:+.t) + ); +} + +1; + From 9879709412e629c67d0881f3862124cb5137243c Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 2 Dec 2013 23:58:52 +0200 Subject: [PATCH 06/56] Modernise Build.PL. Conflicts: META.yml --- Build.PL | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Build.PL b/Build.PL index bddb637..3ab223a 100644 --- a/Build.PL +++ b/Build.PL @@ -8,9 +8,19 @@ use Test::Run::Builder; my $builder = Test::Run::Builder->new( module_name => 'String::Random', license => 'perl', - dist_author => 'Steven Pritchard ', - requires => { + dist_author => 'Shlomi Fish ', + configure_requires => + { + 'Module::Build' => '0.36', + }, + requires => + { + 'Carp' => 0, + 'Exporter' => 0, + 'strict' => 0, 'Test::More' => 0, + 'warnings' => 0, + 'vars' => 0, }, ); From 576870af045704b44c2963c1402b0c8358ce1daf Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 3 Dec 2013 00:13:45 +0200 Subject: [PATCH 07/56] Fix warning on ".". https://rt.cpan.org/Public/Bug/Display.html?id=86894 --- Changes | 2 ++ lib/String/Random.pm | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Changes b/Changes index 66a5d4c..c44c4bb 100644 --- a/Changes +++ b/Changes @@ -4,6 +4,8 @@ Revision history for Perl extension String::Random: - Update the POD in lib/String/Random.pm . - Add t/pod.t . - Convert Build.PL to Test::Run::Builder. +- Hopefully fix https://rt.cpan.org/Public/Bug/Display.html?id=86894 + - Bug #86894 for String-Random: Spurious warnings 0.22 Thu Sep 21 2006 - Fix README to reflect current reality. diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 5335a53..fad1bdc 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -87,8 +87,11 @@ our %regch = ( } elsif (defined($patterns{"\\$tmp"})) { $ch.=$tmp; push(@{$string}, $patterns{$ch}); - } else { - carp "'\\$tmp' being treated as literal '$tmp'"; + } + else { + if ($tmp =~ /\w/) { + carp "'\\$tmp' being treated as literal '$tmp'"; + } push(@{$string}, [$tmp]); } } else { From 64faea1a2dbda8b5bda485335ee737fcde92bbb2 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 3 Dec 2013 00:17:31 +0200 Subject: [PATCH 08/56] Add to the TODO. --- TODO | 3 +++ 1 file changed, 3 insertions(+) diff --git a/TODO b/TODO index 0a95fde..46ba320 100644 --- a/TODO +++ b/TODO @@ -4,3 +4,6 @@ FEATURES: perlre(1), or at least carp() that we aren't doing the right thing. * Allow . to mean binary data in regular expressions (maybe with a flag to new()). +* Implement a way to over-ride the random function using a custom one. + - This will allow platform-independent random number generators + as well as Linux’s /dev/urandom / etc. From 1cef0641f298f2ef9403bdf8e7989d9b55a48894 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 3 Dec 2013 00:19:06 +0200 Subject: [PATCH 09/56] Bump the version to 0.23. --- Changes | 1 + lib/String/Random.pm | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index c44c4bb..4c17013 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,6 @@ Revision history for Perl extension String::Random: +0.23 Tue Dec 03 2013 - Add t/style-trailing-space.t . - Update the POD in lib/String/Random.pm . - Add t/pod.t . diff --git a/lib/String/Random.pm b/lib/String/Random.pm index fad1bdc..586d948 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -27,7 +27,7 @@ our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = (); -our $VERSION = '0.22'; +our $VERSION = '0.23'; # These are the various character sets. our @upper=("A".."Z"); From e1e1b046dbdf36c09b54a06d5753ba0f55d4cc42 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 3 Dec 2013 12:39:28 +0200 Subject: [PATCH 10/56] Add keywords and repository-link. --- Build.PL | 21 ++++++++++++++++++++- Changes | 3 +++ lib/String/Random.pm | 2 +- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Build.PL b/Build.PL index 3ab223a..3acee90 100644 --- a/Build.PL +++ b/Build.PL @@ -13,15 +13,34 @@ my $builder = Test::Run::Builder->new( { 'Module::Build' => '0.36', }, + build_requires => + { + 'Test::More' => 0, + }, requires => { 'Carp' => 0, 'Exporter' => 0, 'strict' => 0, - 'Test::More' => 0, 'warnings' => 0, 'vars' => 0, }, + meta_merge => + { + resources => + { + repository => 'http://github.com/shlomif/perl-String-Random', + }, + keywords => + [ + 'generate', + 'random', + 'regex', + 'regexp', + 'regular-expression', + 'string', + ], + }, ); $builder->create_build_script(); diff --git a/Changes b/Changes index 4c17013..b8a24eb 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension String::Random: +0.24 Tue Dec 03 2013 +- Add keywords and a link to he repository to Build.PL / META.yml. + 0.23 Tue Dec 03 2013 - Add t/style-trailing-space.t . - Update the POD in lib/String/Random.pm . diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 586d948..56f6b85 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -27,7 +27,7 @@ our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = (); -our $VERSION = '0.23'; +our $VERSION = '0.24'; # These are the various character sets. our @upper=("A".."Z"); From 3eeff371e73cef46ad09f4b627f3f80c66c6be4e Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 27 Dec 2013 15:11:54 +0200 Subject: [PATCH 11/56] Fix the link to the repository. --- Build.PL | 2 +- Changes | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Build.PL b/Build.PL index 3acee90..de03edc 100644 --- a/Build.PL +++ b/Build.PL @@ -29,7 +29,7 @@ my $builder = Test::Run::Builder->new( { resources => { - repository => 'http://github.com/shlomif/perl-String-Random', + repository => 'http://github.com/shlomif/String-Random', }, keywords => [ diff --git a/Changes b/Changes index b8a24eb..69869c2 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension String::Random: +0.25 Fri Dec 27 2013 +- Update the link to the version control repository. + 0.24 Tue Dec 03 2013 - Add keywords and a link to he repository to Build.PL / META.yml. From afa262f812e8200999db58ed2b0331f0ee2b0af9 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 27 Dec 2013 15:12:43 +0200 Subject: [PATCH 12/56] Bump the version number to 0.25. --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 56f6b85..462529f 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -27,7 +27,7 @@ our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = (); -our $VERSION = '0.24'; +our $VERSION = '0.25'; # These are the various character sets. our @upper=("A".."Z"); From 99fc5441d264623b51de24a2c3f895c9586997df Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 31 Jan 2014 17:20:59 +0200 Subject: [PATCH 13/56] LICENSE file and section. --- Changes | 3 + LICENSE | 396 +++++++++++++++++++++++++++++++++++++++++++ MANIFEST | 1 + lib/String/Random.pm | 5 + 4 files changed, 405 insertions(+) create mode 100644 LICENSE diff --git a/Changes b/Changes index 69869c2..94cbdcf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension String::Random: +0.26 +- LICENSE section and file (CPANTS). + 0.25 Fri Dec 27 2013 - Update the link to the version control repository. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0ba8d2b --- /dev/null +++ b/LICENSE @@ -0,0 +1,396 @@ +Terms of Perl itself + +a) the GNU General Public License as published by the Free + Software Foundation; either version 1, or (at your option) any + later version, or +b) the "Artistic License" + +---------------------------------------------------------------------------- + + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + +---------------------------------------------------------------------------- + +The Artistic License + +Preamble + +The intent of this document is to state the conditions under which a Package +may be copied, such that the Copyright Holder maintains some semblance of +artistic control over the development of the package, while giving the users of the +package the right to use and distribute the Package in a more-or-less customary +fashion, plus the right to make reasonable modifications. + +Definitions: + +- "Package" refers to the collection of files distributed by the Copyright + Holder, and derivatives of that collection of files created through textual + modification. +- "Standard Version" refers to such a Package if it has not been modified, + or has been modified in accordance with the wishes of the Copyright + Holder. +- "Copyright Holder" is whoever is named in the copyright or copyrights for + the package. +- "You" is you, if you're thinking about copying or distributing this Package. +- "Reasonable copying fee" is whatever you can justify on the basis of + media cost, duplication charges, time of people involved, and so on. (You + will not be required to justify it to the Copyright Holder, but only to the + computing community at large as a market that must bear the fee.) +- "Freely Available" means that no fee is charged for the item itself, though + there may be fees involved in handling the item. It also means that + recipients of the item may redistribute it under the same conditions they + received it. + +1. You may make and give away verbatim copies of the source form of the +Standard Version of this Package without restriction, provided that you duplicate +all of the original copyright notices and associated disclaimers. + +2. You may apply bug fixes, portability fixes and other modifications derived from +the Public Domain or from the Copyright Holder. A Package modified in such a +way shall still be considered the Standard Version. + +3. You may otherwise modify your copy of this Package in any way, provided +that you insert a prominent notice in each changed file stating how and when +you changed that file, and provided that you do at least ONE of the following: + + a) place your modifications in the Public Domain or otherwise + make them Freely Available, such as by posting said modifications + to Usenet or an equivalent medium, or placing the modifications on + a major archive site such as ftp.uu.net, or by allowing the + Copyright Holder to include your modifications in the Standard + Version of the Package. + + b) use the modified Package only within your corporation or + organization. + + c) rename any non-standard executables so the names do not + conflict with standard executables, which must also be provided, + and provide a separate manual page for each non-standard + executable that clearly documents how it differs from the Standard + Version. + + d) make other distribution arrangements with the Copyright Holder. + +4. You may distribute the programs of this Package in object code or executable +form, provided that you do at least ONE of the following: + + a) distribute a Standard Version of the executables and library + files, together with instructions (in the manual page or equivalent) + on where to get the Standard Version. + + b) accompany the distribution with the machine-readable source of + the Package with your modifications. + + c) accompany any non-standard executables with their + corresponding Standard Version executables, giving the + non-standard executables non-standard names, and clearly + documenting the differences in manual pages (or equivalent), + together with instructions on where to get the Standard Version. + + d) make other distribution arrangements with the Copyright Holder. + +5. You may charge a reasonable copying fee for any distribution of this Package. +You may charge any fee you choose for support of this Package. You may not +charge a fee for this Package itself. However, you may distribute this Package in +aggregate with other (possibly commercial) programs as part of a larger +(possibly commercial) software distribution provided that you do not advertise +this Package as a product of your own. + +6. The scripts and library files supplied as input to or produced as output from +the programs of this Package do not automatically fall under the copyright of this +Package, but belong to whomever generated them, and may be sold +commercially, and may be aggregated with this Package. + +7. C or perl subroutines supplied by you and linked into this Package shall not +be considered part of this Package. + +8. The name of the Copyright Holder may not be used to endorse or promote +products derived from this software without specific prior written permission. + +9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED +WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR +PURPOSE. + +The End + diff --git a/MANIFEST b/MANIFEST index 67c14c6..72e236c 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,5 +1,6 @@ Build.PL Changes +LICENSE MANIFEST META.json META.yml diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 462529f..64cf7a7 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -446,6 +446,11 @@ Original Author: Steven Pritchard C<< steve@silug.org >> Now maintained by: Shlomi Fish ( L ). +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + =head1 SEE ALSO perl(1). From 4fb96ca52320156db822da1f01d84eeb598855fa Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 31 Jan 2014 17:23:15 +0200 Subject: [PATCH 14/56] Add minimal version of perl. --- Build.PL | 1 + Changes | 1 + 2 files changed, 2 insertions(+) diff --git a/Build.PL b/Build.PL index de03edc..1767c1b 100644 --- a/Build.PL +++ b/Build.PL @@ -21,6 +21,7 @@ my $builder = Test::Run::Builder->new( { 'Carp' => 0, 'Exporter' => 0, + 'perl' => '5.006', 'strict' => 0, 'warnings' => 0, 'vars' => 0, diff --git a/Changes b/Changes index 94cbdcf..0650ad2 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history for Perl extension String::Random: 0.26 - LICENSE section and file (CPANTS). +- Minimum version of perl in Build.PL / META.yml . 0.25 Fri Dec 27 2013 - Update the link to the version control repository. From 0acfd73cacb2bc96f63a3a869ed149523113b01b Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Fri, 31 Jan 2014 17:24:40 +0200 Subject: [PATCH 15/56] Bump version to 0.26 --- Changes | 2 +- lib/String/Random.pm | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Changes b/Changes index 0650ad2..8b685fe 100644 --- a/Changes +++ b/Changes @@ -1,6 +1,6 @@ Revision history for Perl extension String::Random: -0.26 +0.26 Fri Jan 31 2014 - LICENSE section and file (CPANTS). - Minimum version of perl in Build.PL / META.yml . diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 64cf7a7..92f5a3d 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -27,7 +27,7 @@ our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our @EXPORT = (); -our $VERSION = '0.25'; +our $VERSION = '0.26'; # These are the various character sets. our @upper=("A".."Z"); From 388a510ab4d3b59e8f215d0e6cce617c8068a7c1 Mon Sep 17 00:00:00 2001 From: Kosuke Asami Date: Tue, 29 Apr 2014 14:18:45 +0900 Subject: [PATCH 16/56] Cache parsed range patterns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Measurement: (script) use utf8; my $generator = String::Random->new; $generator->randregex('[一-龠]{5}') for 1..1000; (time) before: 3.63s after: 1.54s (CPU environment) Intel(R) Core(TM) i5-4670 CPU @ 3.40GHz --- lib/String/Random.pm | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 92f5a3d..a7f4d51 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -70,6 +70,9 @@ our %patterns = ( '\e' => [ "\e" ], ); +# This is used for cache of parsed range patterns in %regch +our %parsed_range_patterns = (); + # These characters are treated specially in randregex(). our %regch = ( "\\" => sub { @@ -107,9 +110,19 @@ our %regch = ( my @tmp; while (defined($ch=shift(@{$chars})) && ($ch ne "]")) { if (($ch eq "-") && @{$chars} && @tmp) { + my $begin_ch = $tmp[$#tmp]; $ch=shift(@{$chars}); - for (my $n=ord($tmp[$#tmp]);$n Date: Tue, 29 Apr 2014 09:16:36 +0300 Subject: [PATCH 17/56] Document the latest change. --- Changes | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Changes b/Changes index 8b685fe..7d48d8e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension String::Random: +0.27 +- Cache parsed range patterns. + - Pull Request from Kosuke Asami (co-me). Thanks! + - https://github.com/shlomif/String-Random/pull/1 + 0.26 Fri Jan 31 2014 - LICENSE section and file (CPANTS). - Minimum version of perl in Build.PL / META.yml . From 7b2a8efd7bda75ac9080af800ab5e3e678bfe64a Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 29 Apr 2014 09:25:49 +0300 Subject: [PATCH 18/56] C-style for loop to ranged for. This may be a bit faster and is also cleaner. --- lib/String/Random.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index a7f4d51..ee0812a 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -118,8 +118,8 @@ our %regch = ( } else { my @chs; - for (my $n=ord($begin_ch);$n Date: Tue, 29 Apr 2014 10:09:35 +0300 Subject: [PATCH 19/56] Cleanups and optimisations. --- lib/String/Random.pm | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index ee0812a..0542834 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -147,11 +147,12 @@ our %regch = ( }, '{' => sub { my ($self, $ch, $chars, $string)=@_; - my ($n, $closed); - for ($n=0;$n[$n] eq "}") { - $closed++; - last; + my $closed; + CLOSED: + for my $c (@$chars) { + if ($c eq "}") { + $closed = 1; + last CLOSED; } } if ($closed) { @@ -175,10 +176,9 @@ our %regch = ( } } if ($tmp) { - my $last=$string->[$#{$string}]; - for ($n=0;$n<($tmp-1);$n++) { - push(@{$string}, $last); - } + my $last=$string->[-1]; + + push @$string, (($last) x ($tmp-1)); } else { pop(@{$string}); } @@ -234,7 +234,7 @@ sub randregex { } foreach $ch (@string) { - $string.=$ch->[int(rand(scalar(@{$ch})))]; + $string .= $ch->[ int(rand(scalar(@{$ch}))) ]; } push(@strings, $string); @@ -252,7 +252,7 @@ sub from_pattern { } sub randpattern { - my $self=shift; + my $self = shift; croak "called without a reference" if (!ref($self)); my @strings=(); @@ -281,12 +281,10 @@ sub random_regex { sub random_string { my($pattern,@list)=@_; - my($n,$foo); - - $foo=new String::Random; + my $foo = String::Random->new; - for ($n=0;$n<=$#list;$n++) { - @{$foo->{$n}}=@{$list[$n]}; + for my $n (0 .. $#list) { + $foo->{$n} = [@{$list[$n]}]; } return $foo->randpattern($pattern); From cc29c995510bc92c0f27ff04051679250ed96f01 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 22:00:58 -0500 Subject: [PATCH 20/56] Update formatting using perltidy --- lib/String/Random.pm | 378 +++++++++++++++++++++++-------------------- 1 file changed, 199 insertions(+), 179 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 0542834..7d1f017 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -20,54 +20,59 @@ use warnings; use Carp; use Exporter (); -our @ISA = qw(Exporter); -our %EXPORT_TAGS = ( 'all' => [ qw( - &random_string - &random_regex -) ] ); +our @ISA = qw(Exporter); +our %EXPORT_TAGS = ( + 'all' => [ + qw( + &random_string + &random_regex + ) + ] +); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our @EXPORT = (); -our $VERSION = '0.26'; +our @EXPORT = (); +our $VERSION = '0.26'; # These are the various character sets. -our @upper=("A".."Z"); -our @lower=("a".."z"); -our @digit=("0".."9"); -our @punct=map { chr($_); } (33..47,58..64,91..96,123..126); -our @any=(@upper, @lower, @digit, @punct); -our @salt=(@upper, @lower, @digit, ".", "/"); -our @binary=map { chr($_) } (0..255); +our @upper = ( "A" .. "Z" ); +our @lower = ( "a" .. "z" ); +our @digit = ( "0" .. "9" ); +our @punct = map { chr($_); } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); +our @any = ( @upper, @lower, @digit, @punct ); +our @salt = ( @upper, @lower, @digit, ".", "/" ); +our @binary = map { chr($_) } ( 0 .. 255 ); # What's important is how they relate to the pattern characters. # These are the old patterns for randpattern/random_string. our %old_patterns = ( - 'C' => [ @upper ], - 'c' => [ @lower ], - 'n' => [ @digit ], - '!' => [ @punct ], - '.' => [ @any ], - 's' => [ @salt ], - 'b' => [ @binary ], + 'C' => [@upper], + 'c' => [@lower], + 'n' => [@digit], + '!' => [@punct], + '.' => [@any], + 's' => [@salt], + 'b' => [@binary], ); # These are the regex-based patterns. our %patterns = ( + # These are the regex-equivalents. - '.' => [ @any ], - '\d' => [ @digit ], + '.' => [@any], + '\d' => [@digit], '\D' => [ @upper, @lower, @punct ], '\w' => [ @upper, @lower, @digit, "_" ], '\W' => [ grep { $_ ne "_" } @punct ], - '\s' => [ " ", "\t" ], # Would anything else make sense? + '\s' => [ " ", "\t" ], # Would anything else make sense? '\S' => [ @upper, @lower, @digit, @punct ], # These are translated to their double quoted equivalents. - '\t' => [ "\t" ], - '\n' => [ "\n" ], - '\r' => [ "\r" ], - '\f' => [ "\f" ], - '\a' => [ "\a" ], - '\e' => [ "\e" ], + '\t' => ["\t"], + '\n' => ["\n"], + '\r' => ["\r"], + '\f' => ["\f"], + '\a' => ["\a"], + '\e' => ["\e"], ); # This is used for cache of parsed range patterns in %regch @@ -75,216 +80,231 @@ our %parsed_range_patterns = (); # These characters are treated specially in randregex(). our %regch = ( - "\\" => sub { - my ($self, $ch, $chars, $string)=@_; - if (@{$chars}) { - my $tmp=shift(@{$chars}); - if ($tmp eq "x") { - # This is supposed to be a number in hex, so - # there had better be at least 2 characters left. - $tmp=shift(@{$chars}) . shift(@{$chars}); - push(@{$string}, [chr(hex($tmp))]); - } elsif ($tmp=~/[0-7]/) { - carp "octal parsing not implemented. treating literally."; - push(@{$string}, [$tmp]); - } elsif (defined($patterns{"\\$tmp"})) { - $ch.=$tmp; - push(@{$string}, $patterns{$ch}); - } - else { - if ($tmp =~ /\w/) { - carp "'\\$tmp' being treated as literal '$tmp'"; - } - push(@{$string}, [$tmp]); - } - } else { - croak "regex not terminated"; - } - }, + "\\" => sub { + my ( $self, $ch, $chars, $string ) = @_; + if ( @{$chars} ) { + my $tmp = shift( @{$chars} ); + if ( $tmp eq "x" ) { + + # This is supposed to be a number in hex, so + # there had better be at least 2 characters left. + $tmp = shift( @{$chars} ) . shift( @{$chars} ); + push( @{$string}, [ chr( hex($tmp) ) ] ); + } + elsif ( $tmp =~ /[0-7]/ ) { + carp "octal parsing not implemented. treating literally."; + push( @{$string}, [$tmp] ); + } + elsif ( defined( $patterns{"\\$tmp"} ) ) { + $ch .= $tmp; + push( @{$string}, $patterns{$ch} ); + } + else { + if ( $tmp =~ /\w/ ) { + carp "'\\$tmp' being treated as literal '$tmp'"; + } + push( @{$string}, [$tmp] ); + } + } + else { + croak "regex not terminated"; + } + }, '.' => sub { - my ($self, $ch, $chars, $string)=@_; - push(@{$string}, $patterns{$ch}); - }, + my ( $self, $ch, $chars, $string ) = @_; + push( @{$string}, $patterns{$ch} ); + }, '[' => sub { - my ($self, $ch, $chars, $string)=@_; - my @tmp; - while (defined($ch=shift(@{$chars})) && ($ch ne "]")) { - if (($ch eq "-") && @{$chars} && @tmp) { - my $begin_ch = $tmp[$#tmp]; - $ch=shift(@{$chars}); - my $key = "$begin_ch-$ch"; - if (defined($parsed_range_patterns{$key})) { - push(@tmp, @{$parsed_range_patterns{$key}}); - } - else { - my @chs; - for my $n ((ord($begin_ch)+1) .. ord($ch)) { - push @chs, chr($n); - } - $parsed_range_patterns{$key} = \@chs; - push @tmp, @chs; - } - } else { - carp "'$ch' will be treated literally inside []" - if ($ch=~/\W/); - push(@tmp, $ch); - } - } - croak "unmatched []" if ($ch ne "]"); - push(@{$string}, \@tmp); - }, + my ( $self, $ch, $chars, $string ) = @_; + my @tmp; + while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne "]" ) ) { + if ( ( $ch eq "-" ) && @{$chars} && @tmp ) { + my $begin_ch = $tmp[$#tmp]; + $ch = shift( @{$chars} ); + my $key = "$begin_ch-$ch"; + if ( defined( $parsed_range_patterns{$key} ) ) { + push( @tmp, @{ $parsed_range_patterns{$key} } ); + } + else { + my @chs; + for my $n ( ( ord($begin_ch) + 1 ) .. ord($ch) ) { + push @chs, chr($n); + } + $parsed_range_patterns{$key} = \@chs; + push @tmp, @chs; + } + } + else { + carp "'$ch' will be treated literally inside []" + if ( $ch =~ /\W/ ); + push( @tmp, $ch ); + } + } + croak "unmatched []" if ( $ch ne "]" ); + push( @{$string}, \@tmp ); + }, '*' => sub { - my ($self, $ch, $chars, $string)=@_; - unshift(@{$chars}, split("", "{0,}")); - }, + my ( $self, $ch, $chars, $string ) = @_; + unshift( @{$chars}, split( "", "{0,}" ) ); + }, '+' => sub { - my ($self, $ch, $chars, $string)=@_; - unshift(@{$chars}, split("", "{1,}")); - }, + my ( $self, $ch, $chars, $string ) = @_; + unshift( @{$chars}, split( "", "{1,}" ) ); + }, '?' => sub { - my ($self, $ch, $chars, $string)=@_; - unshift(@{$chars}, split("", "{0,1}")); - }, + my ( $self, $ch, $chars, $string ) = @_; + unshift( @{$chars}, split( "", "{0,1}" ) ); + }, '{' => sub { - my ($self, $ch, $chars, $string)=@_; - my $closed; - CLOSED: - for my $c (@$chars) { - if ($c eq "}") { - $closed = 1; - last CLOSED; - } - } - if ($closed) { - my $tmp; - while (defined($ch=shift(@{$chars})) && ($ch ne "}")) { - croak "'$ch' inside {} not supported" if ($ch!~/[\d,]/); - $tmp.=$ch; - } - if ($tmp=~/,/) { - if (my ($min,$max) = $tmp =~ /^(\d*),(\d*)$/) { - $min = 0 if (!length($min)); - $max = $self->{'_max'} if (!length($max)); - croak "bad range {$tmp}" if ($min>$max); - if ($min == $max) { - $tmp = $min; - } else { - $tmp = $min + int(rand($max - $min +1)); - } - } else { - croak "malformed range {$tmp}"; - } - } - if ($tmp) { - my $last=$string->[-1]; - - push @$string, (($last) x ($tmp-1)); - } else { - pop(@{$string}); - } - } else { - # { isn't closed, so treat it literally. - push(@{$string}, [$ch]); - } - }, + my ( $self, $ch, $chars, $string ) = @_; + my $closed; + CLOSED: + for my $c (@$chars) { + if ( $c eq "}" ) { + $closed = 1; + last CLOSED; + } + } + if ($closed) { + my $tmp; + while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne "}" ) ) { + croak "'$ch' inside {} not supported" if ( $ch !~ /[\d,]/ ); + $tmp .= $ch; + } + if ( $tmp =~ /,/ ) { + if ( my ( $min, $max ) = $tmp =~ /^(\d*),(\d*)$/ ) { + $min = 0 if ( !length($min) ); + $max = $self->{'_max'} if ( !length($max) ); + croak "bad range {$tmp}" if ( $min > $max ); + if ( $min == $max ) { + $tmp = $min; + } + else { + $tmp = $min + int( rand( $max - $min + 1 ) ); + } + } + else { + croak "malformed range {$tmp}"; + } + } + if ($tmp) { + my $last = $string->[-1]; + + push @$string, ( ($last) x ( $tmp - 1 ) ); + } + else { + pop( @{$string} ); + } + } + else { + # { isn't closed, so treat it literally. + push( @{$string}, [$ch] ); + } + }, ); sub new { - my $proto=shift; - my $class=ref($proto) || $proto; + my $proto = shift; + my $class = ref($proto) || $proto; my $self; - $self={ %old_patterns }; # makes $self refer to a copy of %old_patterns - my %args=(); - %args=@_ if (@_); - if (defined($args{'max'})) { - $self->{'_max'}=$args{'max'}; - } else { - $self->{'_max'}=10; + $self = {%old_patterns}; # makes $self refer to a copy of %old_patterns + my %args = (); + %args = @_ if (@_); + if ( defined( $args{'max'} ) ) { + $self->{'_max'} = $args{'max'}; + } + else { + $self->{'_max'} = 10; } - return bless($self, $class); + return bless( $self, $class ); } # Returns a random string for each regular expression given as an # argument, or the strings concatenated when used in a scalar context. sub randregex { - my $self=shift; - croak "called without a reference" if (!ref($self)); + my $self = shift; + croak "called without a reference" if ( !ref($self) ); - my @strings=(); + my @strings = (); - while (defined(my $pattern=shift)) { + while ( defined( my $pattern = shift ) ) { my $ch; - my @string=(); - my $string=''; + my @string = (); + my $string = ''; # Split the characters in the pattern # up into a list for easier parsing. - my @chars=split(//, $pattern); + my @chars = split( //, $pattern ); + + while ( defined( $ch = shift(@chars) ) ) { + if ( defined( $regch{$ch} ) ) { + $regch{$ch}->( $self, $ch, \@chars, \@string ); + } + elsif ( $ch =~ /[\$\^\*\(\)\+\{\}\]\|\?]/ ) { - while (defined($ch=shift(@chars))) { - if (defined($regch{$ch})) { - $regch{$ch}->($self, $ch, \@chars, \@string); - } elsif ($ch=~/[\$\^\*\(\)\+\{\}\]\|\?]/) { # At least some of these probably should have special meaning. carp "'$ch' not implemented. treating literally."; - push(@string, [$ch]); - } else { - push(@string, [$ch]); + push( @string, [$ch] ); + } + else { + push( @string, [$ch] ); } } foreach $ch (@string) { - $string .= $ch->[ int(rand(scalar(@{$ch}))) ]; + $string .= $ch->[ int( rand( scalar( @{$ch} ) ) ) ]; } - push(@strings, $string); + push( @strings, $string ); } - return wantarray ? @strings : join("", @strings); + return wantarray ? @strings : join( "", @strings ); } # For compatibility with an ancient version, please ignore... sub from_pattern { - my $self=shift; - croak "called without a reference" if (!ref($self)); + my $self = shift; + croak "called without a reference" if ( !ref($self) ); return $self->randpattern(@_); } sub randpattern { my $self = shift; - croak "called without a reference" if (!ref($self)); + croak "called without a reference" if ( !ref($self) ); - my @strings=(); + my @strings = (); - while (defined(my $pattern=shift)) { - my $string=''; + while ( defined( my $pattern = shift ) ) { + my $string = ''; - for my $ch (split(//, $pattern)) { - if (defined($self->{$ch})) { - $string.=$self->{$ch}->[int(rand(scalar(@{$self->{$ch}})))]; - } else { + for my $ch ( split( //, $pattern ) ) { + if ( defined( $self->{$ch} ) ) { + $string .= + $self->{$ch}->[ int( rand( scalar( @{ $self->{$ch} } ) ) ) ]; + } + else { croak qq(Unknown pattern character "$ch"!); } } - push(@strings, $string); + push( @strings, $string ); } - return wantarray ? @strings : join("", @strings); + return wantarray ? @strings : join( "", @strings ); } sub random_regex { - my $foo=new String::Random; + my $foo = new String::Random; return $foo->randregex(@_); } sub random_string { - my($pattern,@list)=@_; + my ( $pattern, @list ) = @_; my $foo = String::Random->new; - for my $n (0 .. $#list) { - $foo->{$n} = [@{$list[$n]}]; + for my $n ( 0 .. $#list ) { + $foo->{$n} = [ @{ $list[$n] } ]; } return $foo->randpattern($pattern); From 95833033a1d33d0263c82be15fc13ada67b44704 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 22:05:12 -0500 Subject: [PATCH 21/56] Use lexical loop variables. PBP p.108 --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 7d1f017..b53f5b5 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -251,7 +251,7 @@ sub randregex { } } - foreach $ch (@string) { + foreach my $ch (@string) { $string .= $ch->[ int( rand( scalar( @{$ch} ) ) ) ]; } From 3164b72eeb52f700a46594df1be0aee35b12f405 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 22:16:31 -0500 Subject: [PATCH 22/56] Clean up Exporter stuff 'use parent' instead of modifying @ISA Do not export all symbols by default --- lib/String/Random.pm | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index b53f5b5..1fcc1fd 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -18,9 +18,8 @@ use strict; use warnings; use Carp; -use Exporter (); +use parent qw(Exporter); -our @ISA = qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( @@ -30,7 +29,6 @@ our %EXPORT_TAGS = ( ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our @EXPORT = (); our $VERSION = '0.26'; # These are the various character sets. From 2096f2eda7c619ae8fcceecf3940def5d42ccc38 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 22:21:35 -0500 Subject: [PATCH 23/56] Use negative array index instead of $#var PBP p.88 --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 1fcc1fd..ecd2817 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -117,7 +117,7 @@ our %regch = ( my @tmp; while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne "]" ) ) { if ( ( $ch eq "-" ) && @{$chars} && @tmp ) { - my $begin_ch = $tmp[$#tmp]; + my $begin_ch = $tmp[-1]; $ch = shift( @{$chars} ); my $key = "$begin_ch-$ch"; if ( defined( $parsed_range_patterns{$key} ) ) { From 5e00ada321e1745252b21f43d337041c5bc75868 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 23:09:42 -0500 Subject: [PATCH 24/56] Always unpack @_ first PBP p.178 --- lib/String/Random.pm | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index ecd2817..5cae3ab 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -202,12 +202,12 @@ our %regch = ( ); sub new { - my $proto = shift; + my ( $proto, @args ) = @_; my $class = ref($proto) || $proto; my $self; $self = {%old_patterns}; # makes $self refer to a copy of %old_patterns my %args = (); - %args = @_ if (@_); + %args = @args if (@args); if ( defined( $args{'max'} ) ) { $self->{'_max'} = $args{'max'}; } @@ -261,10 +261,10 @@ sub randregex { # For compatibility with an ancient version, please ignore... sub from_pattern { - my $self = shift; + my ( $self, @args ) = @_; croak "called without a reference" if ( !ref($self) ); - return $self->randpattern(@_); + return $self->randpattern(@args); } sub randpattern { @@ -292,8 +292,9 @@ sub randpattern { } sub random_regex { + my (@args) = @_; my $foo = new String::Random; - return $foo->randregex(@_); + return $foo->randregex(@args); } sub random_string { From e0e83c9eefbeada9098d1ae023c11416ce548aa3 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 23:13:27 -0500 Subject: [PATCH 25/56] Apply perltidy -pbp -nst formatting --- lib/String/Random.pm | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 5cae3ab..9070a64 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -23,9 +23,9 @@ use parent qw(Exporter); our %EXPORT_TAGS = ( 'all' => [ qw( - &random_string - &random_regex - ) + &random_string + &random_regex + ) ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); @@ -134,7 +134,7 @@ our %regch = ( } else { carp "'$ch' will be treated literally inside []" - if ( $ch =~ /\W/ ); + if ( $ch =~ /\W/ ); push( @tmp, $ch ); } } @@ -156,7 +156,7 @@ our %regch = ( '{' => sub { my ( $self, $ch, $chars, $string ) = @_; my $closed; - CLOSED: + CLOSED: for my $c (@$chars) { if ( $c eq "}" ) { $closed = 1; @@ -278,8 +278,8 @@ sub randpattern { for my $ch ( split( //, $pattern ) ) { if ( defined( $self->{$ch} ) ) { - $string .= - $self->{$ch}->[ int( rand( scalar( @{ $self->{$ch} } ) ) ) ]; + $string .= $self->{$ch} + ->[ int( rand( scalar( @{ $self->{$ch} } ) ) ) ]; } else { croak qq(Unknown pattern character "$ch"!); From a19d2ed41c740d99821ee64cd843c87cce23280c Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 23:17:31 -0500 Subject: [PATCH 26/56] Use direct object syntax for new PBP p.349 --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 9070a64..983aac7 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -293,7 +293,7 @@ sub randpattern { sub random_regex { my (@args) = @_; - my $foo = new String::Random; + my $foo = String::Random->new; return $foo->randregex(@args); } From 2084539683cb45c301d25394ced03200befaa810 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Tue, 20 Jan 2015 23:34:11 -0500 Subject: [PATCH 27/56] Use lexical variables instead of package variables PBP pp. 73,75 --- lib/String/Random.pm | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 983aac7..7d7da77 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -32,17 +32,17 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our $VERSION = '0.26'; # These are the various character sets. -our @upper = ( "A" .. "Z" ); -our @lower = ( "a" .. "z" ); -our @digit = ( "0" .. "9" ); -our @punct = map { chr($_); } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); -our @any = ( @upper, @lower, @digit, @punct ); -our @salt = ( @upper, @lower, @digit, ".", "/" ); -our @binary = map { chr($_) } ( 0 .. 255 ); +my @upper = ( "A" .. "Z" ); +my @lower = ( "a" .. "z" ); +my @digit = ( "0" .. "9" ); +my @punct = map { chr($_); } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); +my @any = ( @upper, @lower, @digit, @punct ); +my @salt = ( @upper, @lower, @digit, ".", "/" ); +my @binary = map { chr($_) } ( 0 .. 255 ); # What's important is how they relate to the pattern characters. # These are the old patterns for randpattern/random_string. -our %old_patterns = ( +my %old_patterns = ( 'C' => [@upper], 'c' => [@lower], 'n' => [@digit], @@ -53,7 +53,7 @@ our %old_patterns = ( ); # These are the regex-based patterns. -our %patterns = ( +my %patterns = ( # These are the regex-equivalents. '.' => [@any], @@ -74,10 +74,10 @@ our %patterns = ( ); # This is used for cache of parsed range patterns in %regch -our %parsed_range_patterns = (); +my %parsed_range_patterns = (); # These characters are treated specially in randregex(). -our %regch = ( +my %regch = ( "\\" => sub { my ( $self, $ch, $chars, $string ) = @_; if ( @{$chars} ) { From e4229c0fd6d42f39013dc94a3c20896222c43767 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 02:13:41 -0500 Subject: [PATCH 28/56] Avoid ambiguously named 'last' variables PBP p.48 --- lib/String/Random.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 7d7da77..e5b1582 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -186,9 +186,9 @@ my %regch = ( } } if ($tmp) { - my $last = $string->[-1]; + my $prev_ch = $string->[-1]; - push @$string, ( ($last) x ( $tmp - 1 ) ); + push @$string, ( ($prev_ch) x ( $tmp - 1 ) ); } else { pop( @{$string} ); From 33d661fea9dabbd5c70b9c89d25a46f2b4e503cd Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 21:31:20 -0500 Subject: [PATCH 29/56] Remove useless use of $_ --- lib/String/Random.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index e5b1582..b532d54 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -35,10 +35,10 @@ our $VERSION = '0.26'; my @upper = ( "A" .. "Z" ); my @lower = ( "a" .. "z" ); my @digit = ( "0" .. "9" ); -my @punct = map { chr($_); } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); +my @punct = map { chr } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); my @any = ( @upper, @lower, @digit, @punct ); my @salt = ( @upper, @lower, @digit, ".", "/" ); -my @binary = map { chr($_) } ( 0 .. 255 ); +my @binary = map { chr } ( 0 .. 255 ); # What's important is how they relate to the pattern characters. # These are the old patterns for randpattern/random_string. From d3682072ef1ba7d34a3839c7b5851f7804005881 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 21:39:48 -0500 Subject: [PATCH 30/56] Don't use quotes with strings containing no non-whitespace PBP p. 53 --- lib/String/Random.pm | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index b532d54..cc41f39 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -61,7 +61,7 @@ my %patterns = ( '\D' => [ @upper, @lower, @punct ], '\w' => [ @upper, @lower, @digit, "_" ], '\W' => [ grep { $_ ne "_" } @punct ], - '\s' => [ " ", "\t" ], # Would anything else make sense? + '\s' => [ q{ }, "\t" ], # Would anything else make sense? '\S' => [ @upper, @lower, @digit, @punct ], # These are translated to their double quoted equivalents. @@ -143,15 +143,15 @@ my %regch = ( }, '*' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( "", "{0,}" ) ); + unshift( @{$chars}, split( q{}, "{0,}" ) ); }, '+' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( "", "{1,}" ) ); + unshift( @{$chars}, split( q{}, "{1,}" ) ); }, '?' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( "", "{0,1}" ) ); + unshift( @{$chars}, split( q{}, "{0,1}" ) ); }, '{' => sub { my ( $self, $ch, $chars, $string ) = @_; @@ -228,7 +228,7 @@ sub randregex { while ( defined( my $pattern = shift ) ) { my $ch; my @string = (); - my $string = ''; + my $string = q{}; # Split the characters in the pattern # up into a list for easier parsing. @@ -256,7 +256,7 @@ sub randregex { push( @strings, $string ); } - return wantarray ? @strings : join( "", @strings ); + return wantarray ? @strings : join( q{}, @strings ); } # For compatibility with an ancient version, please ignore... @@ -274,7 +274,7 @@ sub randpattern { my @strings = (); while ( defined( my $pattern = shift ) ) { - my $string = ''; + my $string = q{}; for my $ch ( split( //, $pattern ) ) { if ( defined( $self->{$ch} ) ) { @@ -288,7 +288,7 @@ sub randpattern { push( @strings, $string ); } - return wantarray ? @strings : join( "", @strings ); + return wantarray ? @strings : join( q{}, @strings ); } sub random_regex { From f7c23a83e091722334d7ab4daecb76b176172b12 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 21:42:20 -0500 Subject: [PATCH 31/56] Use regex instead of string in split --- lib/String/Random.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index cc41f39..f585a20 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -143,15 +143,15 @@ my %regch = ( }, '*' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( q{}, "{0,}" ) ); + unshift( @{$chars}, split( //, "{0,}" ) ); }, '+' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( q{}, "{1,}" ) ); + unshift( @{$chars}, split( //, "{1,}" ) ); }, '?' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( q{}, "{0,1}" ) ); + unshift( @{$chars}, split( //, "{0,1}" ) ); }, '{' => sub { my ( $self, $ch, $chars, $string ) = @_; From 5999421e36d370d655cc0f2813c6baba6ec0e537 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 21:43:30 -0500 Subject: [PATCH 32/56] Use brackets with double sigil derefrerence PBP p. 228 --- lib/String/Random.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index f585a20..97421f2 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -157,7 +157,7 @@ my %regch = ( my ( $self, $ch, $chars, $string ) = @_; my $closed; CLOSED: - for my $c (@$chars) { + for my $c (@{$chars}) { if ( $c eq "}" ) { $closed = 1; last CLOSED; @@ -188,7 +188,7 @@ my %regch = ( if ($tmp) { my $prev_ch = $string->[-1]; - push @$string, ( ($prev_ch) x ( $tmp - 1 ) ); + push @{$string}, ( ($prev_ch) x ( $tmp - 1 ) ); } else { pop( @{$string} ); From 11faa057d97e49b91c690547add51f87fb324d34 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 21:58:46 -0500 Subject: [PATCH 33/56] Do not use postfix control 'if' structure PBP pp. 93,94 --- lib/String/Random.pm | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 97421f2..f1529cd 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -171,8 +171,8 @@ my %regch = ( } if ( $tmp =~ /,/ ) { if ( my ( $min, $max ) = $tmp =~ /^(\d*),(\d*)$/ ) { - $min = 0 if ( !length($min) ); - $max = $self->{'_max'} if ( !length($max) ); + if ( !length($min) ) { $min = 0 } + if ( !length($max) ) { $max = $self->{'_max'} } croak "bad range {$tmp}" if ( $min > $max ); if ( $min == $max ) { $tmp = $min; @@ -207,7 +207,7 @@ sub new { my $self; $self = {%old_patterns}; # makes $self refer to a copy of %old_patterns my %args = (); - %args = @args if (@args); + if (@args) { %args = @args } if ( defined( $args{'max'} ) ) { $self->{'_max'} = $args{'max'}; } From a6c8c7df760a3137958897656f3863e500847c7e Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 22:08:44 -0500 Subject: [PATCH 34/56] Useless interpolation of literal string PBP p51 --- lib/String/Random.pm | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index f1529cd..faf171b 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -32,12 +32,12 @@ our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); our $VERSION = '0.26'; # These are the various character sets. -my @upper = ( "A" .. "Z" ); -my @lower = ( "a" .. "z" ); -my @digit = ( "0" .. "9" ); +my @upper = ( 'A' .. 'Z' ); +my @lower = ( 'a' .. 'z' ); +my @digit = ( '0' .. '9' ); my @punct = map { chr } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); my @any = ( @upper, @lower, @digit, @punct ); -my @salt = ( @upper, @lower, @digit, ".", "/" ); +my @salt = ( @upper, @lower, @digit, '.', '/' ); my @binary = map { chr } ( 0 .. 255 ); # What's important is how they relate to the pattern characters. @@ -59,8 +59,8 @@ my %patterns = ( '.' => [@any], '\d' => [@digit], '\D' => [ @upper, @lower, @punct ], - '\w' => [ @upper, @lower, @digit, "_" ], - '\W' => [ grep { $_ ne "_" } @punct ], + '\w' => [ @upper, @lower, @digit, '_' ], + '\W' => [ grep { $_ ne '_' } @punct ], '\s' => [ q{ }, "\t" ], # Would anything else make sense? '\S' => [ @upper, @lower, @digit, @punct ], @@ -78,11 +78,11 @@ my %parsed_range_patterns = (); # These characters are treated specially in randregex(). my %regch = ( - "\\" => sub { + '\\' => sub { my ( $self, $ch, $chars, $string ) = @_; if ( @{$chars} ) { my $tmp = shift( @{$chars} ); - if ( $tmp eq "x" ) { + if ( $tmp eq 'x' ) { # This is supposed to be a number in hex, so # there had better be at least 2 characters left. @@ -90,7 +90,7 @@ my %regch = ( push( @{$string}, [ chr( hex($tmp) ) ] ); } elsif ( $tmp =~ /[0-7]/ ) { - carp "octal parsing not implemented. treating literally."; + carp 'octal parsing not implemented. treating literally.'; push( @{$string}, [$tmp] ); } elsif ( defined( $patterns{"\\$tmp"} ) ) { @@ -105,7 +105,7 @@ my %regch = ( } } else { - croak "regex not terminated"; + croak 'regex not terminated'; } }, '.' => sub { @@ -115,8 +115,8 @@ my %regch = ( '[' => sub { my ( $self, $ch, $chars, $string ) = @_; my @tmp; - while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne "]" ) ) { - if ( ( $ch eq "-" ) && @{$chars} && @tmp ) { + while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne ']' ) ) { + if ( ( $ch eq '-' ) && @{$chars} && @tmp ) { my $begin_ch = $tmp[-1]; $ch = shift( @{$chars} ); my $key = "$begin_ch-$ch"; @@ -138,34 +138,34 @@ my %regch = ( push( @tmp, $ch ); } } - croak "unmatched []" if ( $ch ne "]" ); + croak 'unmatched []' if ( $ch ne ']' ); push( @{$string}, \@tmp ); }, '*' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( //, "{0,}" ) ); + unshift( @{$chars}, split( //, '{0,}' ) ); }, '+' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( //, "{1,}" ) ); + unshift( @{$chars}, split( //, '{1,}' ) ); }, '?' => sub { my ( $self, $ch, $chars, $string ) = @_; - unshift( @{$chars}, split( //, "{0,1}" ) ); + unshift( @{$chars}, split( //, '{0,1}' ) ); }, '{' => sub { my ( $self, $ch, $chars, $string ) = @_; my $closed; CLOSED: for my $c (@{$chars}) { - if ( $c eq "}" ) { + if ( $c eq '}' ) { $closed = 1; last CLOSED; } } if ($closed) { my $tmp; - while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne "}" ) ) { + while ( defined( $ch = shift( @{$chars} ) ) && ( $ch ne '}' ) ) { croak "'$ch' inside {} not supported" if ( $ch !~ /[\d,]/ ); $tmp .= $ch; } @@ -221,7 +221,7 @@ sub new { # argument, or the strings concatenated when used in a scalar context. sub randregex { my $self = shift; - croak "called without a reference" if ( !ref($self) ); + croak 'called without a reference' if ( !ref($self) ); my @strings = (); @@ -262,14 +262,14 @@ sub randregex { # For compatibility with an ancient version, please ignore... sub from_pattern { my ( $self, @args ) = @_; - croak "called without a reference" if ( !ref($self) ); + croak 'called without a reference' if ( !ref($self) ); return $self->randpattern(@args); } sub randpattern { my $self = shift; - croak "called without a reference" if ( !ref($self) ); + croak 'called without a reference' if ( !ref($self) ); my @strings = (); From 284211c7c5f37a47f2f28e4000b2e305902ae906 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 22:14:17 -0500 Subject: [PATCH 35/56] One more perltidy cleanup --- lib/String/Random.pm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index faf171b..4ab2072 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -35,10 +35,10 @@ our $VERSION = '0.26'; my @upper = ( 'A' .. 'Z' ); my @lower = ( 'a' .. 'z' ); my @digit = ( '0' .. '9' ); -my @punct = map { chr } ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); +my @punct = map {chr} ( 33 .. 47, 58 .. 64, 91 .. 96, 123 .. 126 ); my @any = ( @upper, @lower, @digit, @punct ); my @salt = ( @upper, @lower, @digit, '.', '/' ); -my @binary = map { chr } ( 0 .. 255 ); +my @binary = map {chr} ( 0 .. 255 ); # What's important is how they relate to the pattern characters. # These are the old patterns for randpattern/random_string. @@ -60,8 +60,8 @@ my %patterns = ( '\d' => [@digit], '\D' => [ @upper, @lower, @punct ], '\w' => [ @upper, @lower, @digit, '_' ], - '\W' => [ grep { $_ ne '_' } @punct ], - '\s' => [ q{ }, "\t" ], # Would anything else make sense? + '\W' => [ grep { $_ ne '_' } @punct ], + '\s' => [ q{ }, "\t" ], # Would anything else make sense? '\S' => [ @upper, @lower, @digit, @punct ], # These are translated to their double quoted equivalents. @@ -157,7 +157,7 @@ my %regch = ( my ( $self, $ch, $chars, $string ) = @_; my $closed; CLOSED: - for my $c (@{$chars}) { + for my $c ( @{$chars} ) { if ( $c eq '}' ) { $closed = 1; last CLOSED; From c7b87a4e423634f485434b47fe5ffa430894934e Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 22:14:40 -0500 Subject: [PATCH 36/56] Add .perlcriticrc These are the perlcriticrc settings I used to get to a clean bill of health. --- .perlcriticrc | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .perlcriticrc diff --git a/.perlcriticrc b/.perlcriticrc new file mode 100644 index 0000000..7b99964 --- /dev/null +++ b/.perlcriticrc @@ -0,0 +1,29 @@ +## +## Do not use these +# Don't worry about /s on regex +[-RegularExpressions::RequireDotMatchAnything] +# Don't worry about /m on regex +[-RegularExpressions::RequireLineBoundaryMatching] +# Don't worry about /x on regex +[-RegularExpressions::RequireExtendedFormatting] +# Don't worry about magic numbers +[-ValuesAndExpressions::ProhibitMagicNumbers] +# Don't worry about quotes and noisy strings +[-ValuesAndExpressions::ProhibitNoisyQuotes] +# Don't worry about #_ use +[-BuiltinFunctions::ProhibitUselessTopic] +# Don't worry about spelling +[-Documentation::PodSpelling] +# Disagree with this one +[-CodeLayout::ProhibitParensWithBuiltins] +# Don't worry about escaped meta chars +[-RegularExpressions::ProhibitEscapedMetacharacters] +# Don't worry about interpolating meta chars +[-ValuesAndExpressions::RequireInterpolationOfMetachars] + +# +[-Documentation::RequirePodSections] + +[Modules::ProhibitExcessMainComplexity] +max_mccabe = 40 + From fea77057db95b9c8d2a623ed27a0a46cc7979299 Mon Sep 17 00:00:00 2001 From: "Lance A. Brown" Date: Wed, 21 Jan 2015 23:19:27 -0500 Subject: [PATCH 37/56] Add rand_gen parameter to new rand_gen specifies a subroutine coderef that accepts one argument, $max, and return an integer between 0 and $max-1. --- lib/String/Random.pm | 30 +++++++++++++++++++++++++++--- t/07_rand_gen.t | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 t/07_rand_gen.t diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 4ab2072..a096aa1 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -178,7 +178,7 @@ my %regch = ( $tmp = $min; } else { - $tmp = $min + int( rand( $max - $min + 1 ) ); + $tmp = $min + $self->{'_rand'}( $max - $min + 1 ); } } else { @@ -201,6 +201,12 @@ my %regch = ( }, ); +# Default rand function +sub _rand { + my ($max) = @_; + return int rand $max; +} + sub new { my ( $proto, @args ) = @_; my $class = ref($proto) || $proto; @@ -214,6 +220,12 @@ sub new { else { $self->{'_max'} = 10; } + if ( defined( $args{'rand_gen'} ) ) { + $self->{'_rand'} = $args{'rand_gen'}; + } + else { + $self->{'_rand'} = sub { _rand(@_) } + } return bless( $self, $class ); } @@ -250,7 +262,7 @@ sub randregex { } foreach my $ch (@string) { - $string .= $ch->[ int( rand( scalar( @{$ch} ) ) ) ]; + $string .= $ch->[ $self->{'_rand'}( scalar( @{$ch} ) ) ]; } push( @strings, $string ); @@ -279,7 +291,7 @@ sub randpattern { for my $ch ( split( //, $pattern ) ) { if ( defined( $self->{$ch} ) ) { $string .= $self->{$ch} - ->[ int( rand( scalar( @{ $self->{$ch} } ) ) ) ]; + ->[ $self->{'_rand'}( scalar( @{ $self->{$ch} } ) ) ]; } else { croak qq(Unknown pattern character "$ch"!); @@ -396,12 +408,24 @@ for adding patterns. =item new max =E I +=item new rand_gen =E I + Create a new String::Random object. Optionally a parameter C can be included to specify the maximum number of characters to return for C<*> and other regular expression patterns that do not return a fixed number of characters. +Optionally a parameter C can be included to specify a subroutine +coderef for generating the random numbers used in this module. The coderef +must accept one argument C and return an integer between 0 and C. +The default rand_gen coderef is + + sub { + my ($max) = @_; + return int rand $max; + } + =item randpattern LIST The randpattern method returns a random string based on the concatenation diff --git a/t/07_rand_gen.t b/t/07_rand_gen.t new file mode 100644 index 0000000..48dde09 --- /dev/null +++ b/t/07_rand_gen.t @@ -0,0 +1,33 @@ +use strict; +use warnings; + +use Test::More tests => (6); + +# 1: Make sure we can load the module +BEGIN { use_ok('String::Random'); } + +# 2: Make sure we can create a new object with rand_gen argument +my $foo = String::Random->new( + rand_gen => sub { + my ($max) = @_; + return int( $max - 1 ); + } +); +ok( defined($foo), "new()" ); + +# 3: Make sure _rand is defined +ok( defined( $foo->{'_rand'} ), "_rand defined" ); + +# 4: Make sure _rand returns value as expected +is( $foo->{'_rand'}(10), 9 ) + or diag "_rand function returned wrong value"; + +# 5: check randpattern with rand_gen function +my $cCn = $foo->randpattern("cCn"); +is( $cCn, 'zZ9', "randpattern() with rand_gen" ); + +# 6: check randregex with rand_gen function +$cCn = $foo->randregex("[a-z][A-Z][0-9]"); +is( $cCn, 'zZ9', "randregex() with rand_gen" ); + +# vi: set ai et syntax=perl: From 2cbaca4e89c66d156e8681bf9d4f894212cf9768 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 09:39:50 +0200 Subject: [PATCH 38/56] Add t/07_rand_gen.t to the MANIFEST. This way it will be included ./Build dist | ./Build disttest. --- MANIFEST | 1 + 1 file changed, 1 insertion(+) diff --git a/MANIFEST b/MANIFEST index 72e236c..ae7fc2d 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,5 +14,6 @@ t/03_random_string.t t/04_randpattern.t t/05_randregex.t t/06_random_regex.t +t/07_rand_gen.t t/pod.t t/style-trailing-space.t From 8fa24aeb704e466f2fa74e9e55bfbb90d3b475f5 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 09:49:32 +0200 Subject: [PATCH 39/56] update the Changes. --- Changes | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index 7d48d8e..e2ffc96 100644 --- a/Changes +++ b/Changes @@ -1,9 +1,15 @@ Revision history for Perl extension String::Random: -0.27 +0.27 Thu Jan 22 2015 - Cache parsed range patterns. - Pull Request from Kosuke Asami (co-me). Thanks! - https://github.com/shlomif/String-Random/pull/1 +- Clean up the code according to perlcritic. + - Pull Request from Lance Brown. Thanks! + - https://github.com/shlomif/String-Random/pull/2 +- Add rand_gen parameter to new to specify a custom random generator. + - Pull Request from Lance Brown. Thanks! + - https://github.com/shlomif/String-Random/pull/3 0.26 Fri Jan 31 2014 - LICENSE section and file (CPANTS). From a0e9bc342763cab5fda3dd0d34a66dbf0bdfefde Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 09:51:27 +0200 Subject: [PATCH 40/56] Small optimisation to an earlier commit. Avoid indirection in the _rand. --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index a096aa1..f48c723 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -224,7 +224,7 @@ sub new { $self->{'_rand'} = $args{'rand_gen'}; } else { - $self->{'_rand'} = sub { _rand(@_) } + $self->{'_rand'} = \&_rand; } return bless( $self, $class ); } From 5f80a76f46e16b7bc97d309a63d812439ed0d74d Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 09:53:35 +0200 Subject: [PATCH 41/56] Bump version to 0.27. --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index f48c723..7c18b49 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -29,7 +29,7 @@ our %EXPORT_TAGS = ( ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our $VERSION = '0.26'; +our $VERSION = '0.27'; # These are the various character sets. my @upper = ( 'A' .. 'Z' ); From 0f26ba8f5dd09e22dfcc9085afda6ecc5b435fa9 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 10:23:01 +0200 Subject: [PATCH 42/56] Convert Changes to CPAN::Changes. --- Changes | 175 +++++++++++++++++++++++------------------------ MANIFEST | 1 + t/cpan-changes.t | 12 ++++ 3 files changed, 100 insertions(+), 88 deletions(-) create mode 100644 t/cpan-changes.t diff --git a/Changes b/Changes index e2ffc96..5d09d4e 100644 --- a/Changes +++ b/Changes @@ -1,90 +1,89 @@ Revision history for Perl extension String::Random: -0.27 Thu Jan 22 2015 -- Cache parsed range patterns. - - Pull Request from Kosuke Asami (co-me). Thanks! - - https://github.com/shlomif/String-Random/pull/1 -- Clean up the code according to perlcritic. - - Pull Request from Lance Brown. Thanks! - - https://github.com/shlomif/String-Random/pull/2 -- Add rand_gen parameter to new to specify a custom random generator. - - Pull Request from Lance Brown. Thanks! - - https://github.com/shlomif/String-Random/pull/3 - -0.26 Fri Jan 31 2014 -- LICENSE section and file (CPANTS). -- Minimum version of perl in Build.PL / META.yml . - -0.25 Fri Dec 27 2013 -- Update the link to the version control repository. - -0.24 Tue Dec 03 2013 -- Add keywords and a link to he repository to Build.PL / META.yml. - -0.23 Tue Dec 03 2013 -- Add t/style-trailing-space.t . -- Update the POD in lib/String/Random.pm . -- Add t/pod.t . -- Convert Build.PL to Test::Run::Builder. -- Hopefully fix https://rt.cpan.org/Public/Bug/Display.html?id=86894 - - Bug #86894 for String-Random: Spurious warnings - -0.22 Thu Sep 21 2006 -- Fix README to reflect current reality. -- Random cleanup in this file. -- Since we've broken compatibility with *really* old Perl5 anyway, - modernize a bit more ("our" instead of "use vars"). Also - explicitly require 5.6.1. - -0.21 Thu Apr 20 2006 -- Modify test.pl to use Test::More. -- Build with Module::Build. -- Minor whitespace cleanup. -- Avoid undefined results from randregex() and randpattern(). -- Turn on warnings in the module. - -0.20 Mon Sep 29 2003 -- Added support for *, +, and ? in regular expressions. - -0.1992 Thu Jul 25 2002 -- Added support for {n,m} ranges in regular expressions. -- Cleaned up the initialization of @punct. - -0.1991 Thu Feb 28 2002 -- Added a "b" for random binary data in randpattern(). - -0.199 Tue Feb 26 2002 -- fixed randregex() and randpattern() to Do The Right Thing(TM) - when called in an array or scalar context, and when passed - multiple arguments. - (* WARNING * This is an incompatible change. Code expecting the - behavior of earlier versions may break. Of course, the documentation - warned that this change would be made, and everybody reads - documentation, right? :-) -- fixed use of $_ in randpattern(). - (Unfortunately, there's still one use of $_ in a grep(), but I don't - see how that can be avoided, and it only happens once on - initialization.) - -0.198 Tue May 16 2000 -- fixed \W generating "_" -- reorganized a bit to make it easier to add more patterns -- added \s and \S -- added two-character literals (\t, \n, \r, \f, \a, \e) - -0.197 Sat Jul 10 1999 -- moved most of randregex() to %regch -- added random_regex() function interface - -0.195 Wed Jul 7 1999 -- regex support is (at least mostly) complete - -0.19 Sun Jul 4 1999 -- first pass of regex support - -0.1 Thu Sep 10 1998 -- original version - -$Id: Changes,v 1.13 2006/09/21 17:33:58 steve Exp $ - -# vi: set ai et: +0.28 2015-01-22 + - Convert Changes to CPAN::Changes and add t/cpan-changes.t . + +0.27 2015-01-22 + - Cache parsed range patterns. + - Pull Request from Kosuke Asami (co-me). Thanks! + - https://github.com/shlomif/String-Random/pull/1 + - Clean up the code according to perlcritic. + - Pull Request from Lance Brown. Thanks! + - https://github.com/shlomif/String-Random/pull/2 + - Add rand_gen parameter to new to specify a custom random generator. + - Pull Request from Lance Brown. Thanks! + - https://github.com/shlomif/String-Random/pull/3 + +0.26 2014-01-31 + - LICENSE section and file (CPANTS). + - Minimum version of perl in Build.PL / META.yml . + +0.25 2013-12-27 + - Update the link to the version control repository. + +0.24 2013-12-03 + - Add keywords and a link to he repository to Build.PL / META.yml. + +0.23 2013-12-03 + - Add t/style-trailing-space.t . + - Update the POD in lib/String/Random.pm . + - Add t/pod.t . + - Convert Build.PL to Test::Run::Builder. + - Hopefully fix https://rt.cpan.org/Public/Bug/Display.html?id=86894 + - Bug #86894 for String-Random: Spurious warnings + +0.22 2006-09-21 + - Fix README to reflect current reality. + - Random cleanup in this file. + - Since we've broken compatibility with *really* old Perl5 anyway, + modernize a bit more ("our" instead of "use vars"). Also + explicitly require 5.6.1. + +0.21 2006-04-20 + - Modify test.pl to use Test::More. + - Build with Module::Build. + - Minor whitespace cleanup. + - Avoid undefined results from randregex() and randpattern(). + - Turn on warnings in the module. + +0.20 2003-09-29 + - Added support for *, +, and ? in regular expressions. + +0.1992 2002-07-25 + - Added support for {n,m} ranges in regular expressions. + - Cleaned up the initialization of @punct. + +0.1991 2002-02-28 + - Added a "b" for random binary data in randpattern(). + +0.199 2002-02-26 + - fixed randregex() and randpattern() to Do The Right Thing(TM) + when called in an array or scalar context, and when passed + multiple arguments. + (* WARNING * This is an incompatible change. Code expecting the + behavior of earlier versions may break. Of course, the documentation + warned that this change would be made, and everybody reads + documentation, right? :-) + - fixed use of $_ in randpattern(). + (Unfortunately, there's still one use of $_ in a grep(), but I don't + see how that can be avoided, and it only happens once on + initialization.) + +0.198 2000-05-16 + - fixed \W generating "_" + - reorganized a bit to make it easier to add more patterns + - added \s and \S + - added two-character literals (\t, \n, \r, \f, \a, \e) + +0.197 1999-07-10 + - moved most of randregex() to %regch + - added random_regex() function interface + +0.195 1999-07-07 + - regex support is (at least mostly) complete + +0.19 1999-07-04 + - first pass of regex support + +0.1 1998-09-10 + - original version diff --git a/MANIFEST b/MANIFEST index ae7fc2d..776ca85 100644 --- a/MANIFEST +++ b/MANIFEST @@ -15,5 +15,6 @@ t/04_randpattern.t t/05_randregex.t t/06_random_regex.t t/07_rand_gen.t +t/cpan-changes.t t/pod.t t/style-trailing-space.t diff --git a/t/cpan-changes.t b/t/cpan-changes.t new file mode 100644 index 0000000..5ec3b98 --- /dev/null +++ b/t/cpan-changes.t @@ -0,0 +1,12 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +eval 'use Test::CPAN::Changes'; +plan skip_all => 'Test::CPAN::Changes required for this test' if $@; + +changes_ok(); + From dc1d8c22c6e414769bbba50fc3c4eb511f9c33ed Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 22 Jan 2015 10:23:53 +0200 Subject: [PATCH 43/56] Bump version to 0.28. --- lib/String/Random.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 7c18b49..9b1a265 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -29,7 +29,7 @@ our %EXPORT_TAGS = ( ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our $VERSION = '0.27'; +our $VERSION = '0.28'; # These are the various character sets. my @upper = ( 'A' .. 'Z' ); From 653100777de7957cf3aa07633fd49f5f7af105e0 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 24 Dec 2015 11:56:12 +0200 Subject: [PATCH 44/56] Add dependency on 'parent'. --- Build.PL | 1 + Changes | 5 +++++ lib/String/Random.pm | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Build.PL b/Build.PL index 1767c1b..6677a0d 100644 --- a/Build.PL +++ b/Build.PL @@ -21,6 +21,7 @@ my $builder = Test::Run::Builder->new( { 'Carp' => 0, 'Exporter' => 0, + 'parent' => 0, 'perl' => '5.006', 'strict' => 0, 'warnings' => 0, diff --git a/Changes b/Changes index 5d09d4e..993f0e1 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension String::Random: +0.29 2015-12-24 + - Add a dependency on the 'parent' module: + - This caused some CPAN Testers failures on perl-5.8.x. + - http://www.cpantesters.org/cpan/report/d21f0078-6c11-1014-b233-6b3058476d35 + 0.28 2015-01-22 - Convert Changes to CPAN::Changes and add t/cpan-changes.t . diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 9b1a265..1c56f16 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -29,7 +29,7 @@ our %EXPORT_TAGS = ( ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our $VERSION = '0.28'; +our $VERSION = '0.29'; # These are the various character sets. my @upper = ( 'A' .. 'Z' ); From e3ffb850a9fc410e08d45f65a3da4196e4aa8e01 Mon Sep 17 00:00:00 2001 From: Joel Maslak Date: Wed, 22 Jun 2016 19:36:30 -0400 Subject: [PATCH 45/56] Add note about how to generate a secure random pattern --- lib/String/Random.pm | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 1c56f16..2961c81 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -360,8 +360,11 @@ This would output something like this: Your password is UDwp$tj5 -B: currently, String::Random uses Perl's built-in predictable random -number generator so the passwords generated by it are insecure. +B: currently, C defaults to Perl's built-in predictable +random number generator so the passwords generated by it are insecure. See the +C option to C constructor to specify a more secure +random number generator. There is no equivilent to this in the imparative +interface, you must use the OO interface to get this functionality. If you are more comfortable dealing with regular expressions, the following code would have a similar result: From c9fcbb02053a251306e2852c8529bbe501ce3008 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 23 Jun 2016 14:29:27 +0300 Subject: [PATCH 46/56] Correct some typos in the last commit. --- lib/String/Random.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 2961c81..ee945ed 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -363,8 +363,9 @@ This would output something like this: B: currently, C defaults to Perl's built-in predictable random number generator so the passwords generated by it are insecure. See the C option to C constructor to specify a more secure -random number generator. There is no equivilent to this in the imparative -interface, you must use the OO interface to get this functionality. +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: From cab9cad8aec416f4f7e66082b39d7406b53dd17e Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Thu, 23 Jun 2016 14:40:02 +0300 Subject: [PATCH 47/56] Add a test for POD coverage. --- MANIFEST | 1 + lib/String/Random.pm | 9 +++++++++ t/pod-coverage.t | 6 ++++++ 3 files changed, 16 insertions(+) create mode 100644 t/pod-coverage.t diff --git a/MANIFEST b/MANIFEST index 776ca85..1d17ac2 100644 --- a/MANIFEST +++ b/MANIFEST @@ -17,4 +17,5 @@ t/06_random_regex.t t/07_rand_gen.t t/cpan-changes.t t/pod.t +t/pod-coverage.t t/style-trailing-space.t diff --git a/lib/String/Random.pm b/lib/String/Random.pm index ee945ed..7d688c5 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -464,6 +464,10 @@ characters inside [] are not supported (with the exception of "-" to denote ranges of characters). The parser doesn't care for spaces in the "regular expression" either. +=item from_pattern + +B - for compatibility with an old version. B + =back =head2 Functions @@ -488,6 +492,11 @@ would print something like this: cebd +=item random_regex REGEX_IN_STRING + +Prints a string for the regular expression given as the string. See the +synposis for example. + =back =head1 BUGS diff --git a/t/pod-coverage.t b/t/pod-coverage.t new file mode 100644 index 0000000..703f91d --- /dev/null +++ b/t/pod-coverage.t @@ -0,0 +1,6 @@ +#!perl -T + +use Test::More; +eval "use Test::Pod::Coverage 1.04"; +plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; +all_pod_coverage_ok(); From 4cef05ce627ef55032ae7dd3acc30c3811837882 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 22 May 2018 20:55:33 +0300 Subject: [PATCH 48/56] Convert to dzil --- .tidyallrc | 3 +++ Build.PL | 50 ---------------------------------------- Changes | 3 +++ MANIFEST | 21 ----------------- MANIFEST.SKIP | 1 + README | 29 ----------------------- dist.ini | 19 +++++++++++++++ lib/String/Random.pm | 1 - t/cpan-changes.t | 12 ---------- t/pod-coverage.t | 6 ----- t/pod.t | 12 ---------- t/style-trailing-space.t | 30 ------------------------ weaver.ini | 39 +++++++++++++++++++++++++++++++ 13 files changed, 65 insertions(+), 161 deletions(-) create mode 100644 .tidyallrc delete mode 100644 Build.PL delete mode 100644 MANIFEST create mode 100644 MANIFEST.SKIP delete mode 100644 README create mode 100644 dist.ini delete mode 100644 t/cpan-changes.t delete mode 100644 t/pod-coverage.t delete mode 100644 t/pod.t delete mode 100644 t/style-trailing-space.t create mode 100644 weaver.ini diff --git a/.tidyallrc b/.tidyallrc new file mode 100644 index 0000000..b62c3dc --- /dev/null +++ b/.tidyallrc @@ -0,0 +1,3 @@ +[PerlTidy] +argv = -ci=4 -bl -cti=0 +select = {t,lib,scripts}/**/*.{pl,pm,t} {t,lib,scripts}/*.{pl,pm,t} diff --git a/Build.PL b/Build.PL deleted file mode 100644 index 6677a0d..0000000 --- a/Build.PL +++ /dev/null @@ -1,50 +0,0 @@ -use strict; -use warnings; - -use lib './inc'; - -use Test::Run::Builder; - -my $builder = Test::Run::Builder->new( - module_name => 'String::Random', - license => 'perl', - dist_author => 'Shlomi Fish ', - configure_requires => - { - 'Module::Build' => '0.36', - }, - build_requires => - { - 'Test::More' => 0, - }, - requires => - { - 'Carp' => 0, - 'Exporter' => 0, - 'parent' => 0, - 'perl' => '5.006', - 'strict' => 0, - 'warnings' => 0, - 'vars' => 0, - }, - meta_merge => - { - resources => - { - repository => 'http://github.com/shlomif/String-Random', - }, - keywords => - [ - 'generate', - 'random', - 'regex', - 'regexp', - 'regular-expression', - 'string', - ], - }, -); - -$builder->create_build_script(); - -# vi: set ai et: diff --git a/Changes b/Changes index 993f0e1..3140faf 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,8 @@ Revision history for Perl extension String::Random: +0.30 2018-05-22 + - Convert to Dist-Zilla. + 0.29 2015-12-24 - Add a dependency on the 'parent' module: - This caused some CPAN Testers failures on perl-5.8.x. diff --git a/MANIFEST b/MANIFEST deleted file mode 100644 index 1d17ac2..0000000 --- a/MANIFEST +++ /dev/null @@ -1,21 +0,0 @@ -Build.PL -Changes -LICENSE -MANIFEST -META.json -META.yml -README -TODO -inc/Test/Run/Builder.pm -lib/String/Random.pm -t/01_use.t -t/02_new.t -t/03_random_string.t -t/04_randpattern.t -t/05_randregex.t -t/06_random_regex.t -t/07_rand_gen.t -t/cpan-changes.t -t/pod.t -t/pod-coverage.t -t/style-trailing-space.t diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..2e58f07 --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1 @@ +~$ diff --git a/README b/README deleted file mode 100644 index 287e754..0000000 --- a/README +++ /dev/null @@ -1,29 +0,0 @@ -String::Random is used to generate random strings. It was written to -make generating random passwords and such a little easier. See the -documentation in pod format in the module for more information. - -String::Random now requires Module::Build to build and install. -To install the module, simply do the following: - - perl Build.PL - ./Build - ./Build test - ./Build install - -This module requires Perl5. Any version newer than 5.6.0 should work, -but it has only been tested on recent versions. Version 0.20 and -earlier of this module should work with any Perl5 release. You can -find older releases on backpan: - - http://backpan.cpan.org/authors/id/S/ST/STEVE/ - - ****** PLEASE NOTE ****** - -This module is still somewhat experimental. I'm trying to determine -exactly what the feature set of the module should be. Please send -suggestions, bug reports, comments, etc. to the author, Steven -Pritchard . - -Copyright (C) 1999-2006 Steven Pritchard -This program is free software; you can redistribute it -and/or modify it under the same terms as Perl itself. diff --git a/dist.ini b/dist.ini new file mode 100644 index 0000000..c6bdb89 --- /dev/null +++ b/dist.ini @@ -0,0 +1,19 @@ +name = String-Random +version = 0.30 +author = Shlomi Fish +license = Perl_5 +copyright_holder = Shlomi Fish + +[@Filter] +-bundle = @SHLOMIF +-remove = License +-remove = MakeMaker +dist = String-Random +repository_url = http://github.com/shlomif/String-Random +[Keywords] +keyword = generate +keyword = random +keyword = regex +keyword = regexp +keyword = regular-expression +keyword = string diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 7d688c5..2ef941a 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -29,7 +29,6 @@ our %EXPORT_TAGS = ( ] ); our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); -our $VERSION = '0.29'; # These are the various character sets. my @upper = ( 'A' .. 'Z' ); diff --git a/t/cpan-changes.t b/t/cpan-changes.t deleted file mode 100644 index 5ec3b98..0000000 --- a/t/cpan-changes.t +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More; - -eval 'use Test::CPAN::Changes'; -plan skip_all => 'Test::CPAN::Changes required for this test' if $@; - -changes_ok(); - diff --git a/t/pod-coverage.t b/t/pod-coverage.t deleted file mode 100644 index 703f91d..0000000 --- a/t/pod-coverage.t +++ /dev/null @@ -1,6 +0,0 @@ -#!perl -T - -use Test::More; -eval "use Test::Pod::Coverage 1.04"; -plan skip_all => "Test::Pod::Coverage 1.04 required for testing POD coverage" if $@; -all_pod_coverage_ok(); diff --git a/t/pod.t b/t/pod.t deleted file mode 100644 index ee8b18a..0000000 --- a/t/pod.t +++ /dev/null @@ -1,12 +0,0 @@ -#!perl -T - -use strict; -use warnings; -use Test::More; - -# Ensure a recent version of Test::Pod -my $min_tp = 1.22; -eval "use Test::Pod $min_tp"; -plan skip_all => "Test::Pod $min_tp required for testing POD" if $@; - -all_pod_files_ok(); diff --git a/t/style-trailing-space.t b/t/style-trailing-space.t deleted file mode 100644 index 131a278..0000000 --- a/t/style-trailing-space.t +++ /dev/null @@ -1,30 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More; - -eval "use Test::TrailingSpace"; -if ($@) -{ - plan skip_all => "Test::TrailingSpace required for trailing space test."; -} -else -{ - plan tests => 1; -} - -# TODO: add .pod, .PL, the README/Changes/TODO/etc. documents and possibly -# some other stuff. -my $finder = Test::TrailingSpace->new( - { - root => '.', - filename_regex => qr/(?:\.(?:t|pm|pl|xs|c|h|txt|pod|PL)|README|Changes|TODO|LICENSE)\z/, - }, -); - -# TEST -$finder->no_trailing_space( - "No trailing space was found." -); diff --git a/weaver.ini b/weaver.ini new file mode 100644 index 0000000..228ad8e --- /dev/null +++ b/weaver.ini @@ -0,0 +1,39 @@ +[@CorePrep] + +[-SingleEncoding] + +[Generic / NAME] + +[Version] + +[Region / prelude] + + +[Generic / SYNOPSIS] +[Generic / DESCRIPTION] +[Generic / OVERVIEW] + +[Collect / ATTRIBUTES] +command = attr + +[Collect / METHODS] +command = method + +[Leftovers] + +[Region / postlude] + +[Authors] +[Legal] + +; [Generic / DESCRIPTION] +; required = 1 + +; [Generic / BUGS] + +; [Generic / Section::Bugs] +; [Generic / Section::License] +; +[Bugs] +[Support] +all_modules = 1 From 9465a9da63bd7172724bca6d1887d80930561aae Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sat, 2 Jun 2018 01:19:57 +0300 Subject: [PATCH 49/56] Add README file for GitHub . README and LICENSE files are a git/hg/etc. and GitHub best practice. See: * https://help.github.com/articles/about-readmes/ * https://help.github.com/articles/licensing-a-repository/ --- README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..57d87a3 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +[String-Random](https://metacpan.org/release/String-Random) on CPAN. From 788ee8ae34d539c69f65e3e12ac90d56f9ed62cd Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sat, 2 Jun 2018 16:51:54 +0300 Subject: [PATCH 50/56] Add travis-ci. --- .travis.yml | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6c222b5 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,21 @@ +sudo: false +language: perl +perl: + - '5.26' + - '5.24' + - '5.22' + - '5.20' + - 'blead' +matrix: + allow_failures: + - perl: 'blead' + fast_finish: true +before_install: + - git config --global user.name "TravisCI" + - git config --global user.email $HOSTNAME":not-for-mail@travis-ci.org" +install: + - cpanm --quiet --notest --skip-satisfied Dist::Zilla Pod::Weaver::Section::Support + - "(cd . && dzil authordeps --missing | grep -vP '[^\\w:]' | xargs -n 5 -P 10 cpanm --quiet --notest)" + - "(cd . && dzil listdeps --author --missing | grep -vP '[^\\w:]' | cpanm --verbose)" +script: + - "cd . && dzil smoke --release --author" From abc47bf7709681b85945286240d3aa36a0f7d086 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Wed, 7 Oct 2020 13:02:57 +0300 Subject: [PATCH 51/56] Fix tidyall tests. See: https://metacpan.org/release/Code-TidyAll . --- .tidyallrc | 9 ++++++++- weaver.ini | 40 +--------------------------------------- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/.tidyallrc b/.tidyallrc index b62c3dc..f728753 100644 --- a/.tidyallrc +++ b/.tidyallrc @@ -1,3 +1,10 @@ +[PerlCritic] +argv = +select = {t,lib,scripts}/**/*.{pl,pm,t} {t,lib,scripts}/*.{pl,pm,t} + [PerlTidy] -argv = -ci=4 -bl -cti=0 +argv = -ci=4 -bl -cti=0 --character-encoding=none select = {t,lib,scripts}/**/*.{pl,pm,t} {t,lib,scripts}/*.{pl,pm,t} + +[TestCount] +select = t/*.t diff --git a/weaver.ini b/weaver.ini index 228ad8e..21ba738 100644 --- a/weaver.ini +++ b/weaver.ini @@ -1,39 +1 @@ -[@CorePrep] - -[-SingleEncoding] - -[Generic / NAME] - -[Version] - -[Region / prelude] - - -[Generic / SYNOPSIS] -[Generic / DESCRIPTION] -[Generic / OVERVIEW] - -[Collect / ATTRIBUTES] -command = attr - -[Collect / METHODS] -command = method - -[Leftovers] - -[Region / postlude] - -[Authors] -[Legal] - -; [Generic / DESCRIPTION] -; required = 1 - -; [Generic / BUGS] - -; [Generic / Section::Bugs] -; [Generic / Section::License] -; -[Bugs] -[Support] -all_modules = 1 +[@SHLOMIF] From 49dc8246437ac450f417967bf44d7fd2a4928502 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Sat, 17 Oct 2020 15:13:38 +0300 Subject: [PATCH 52/56] remove annocpan link; dist.ini cleanups --- Changes | 5 +++++ dist.ini | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index 3140faf..994cb87 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension String::Random: +0.31 2020-10-17 + - dist.ini / weaver.ini / .tidyallrc / etc. cleanup + - Move to @SHLOMIF + - Inspired by Lady_Aleena. + 0.30 2018-05-22 - Convert to Dist-Zilla. diff --git a/dist.ini b/dist.ini index c6bdb89..12b2aec 100644 --- a/dist.ini +++ b/dist.ini @@ -1,5 +1,5 @@ name = String-Random -version = 0.30 +version = 0.31 author = Shlomi Fish license = Perl_5 copyright_holder = Shlomi Fish From 8db46026326c238582a0e9af42032ab377ffc3f8 Mon Sep 17 00:00:00 2001 From: Gene Boggs Date: Mon, 13 Sep 2021 15:19:36 -0700 Subject: [PATCH 53/56] Add the get_ & set_pattern methods --- lib/String/Random.pm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 2ef941a..2a43c74 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -302,6 +302,16 @@ sub randpattern { return wantarray ? @strings : join( q{}, @strings ); } +sub get_pattern { + my ( $self, $name ) = @_; + return $self->{ $name }; +} + +sub set_pattern { + my ( $self, $name, $charset ) = @_; + $self->{ $name } = $charset; +} + sub random_regex { my (@args) = @_; my $foo = String::Random->new; @@ -400,6 +410,11 @@ I my $gen = String::Random->new; $gen->{'A'} = [ @{$gen->{'C'}}, @{$gen->{'c'}} ]; +I + + 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. @@ -463,6 +478,20 @@ characters inside [] are not supported (with the exception of "-" to denote ranges of characters). The parser doesn't care for spaces in the "regular expression" either. +=item get_pattern STRING + +Return a pattern given a name. + + my $gen = String::Random->new; + $gen->get_pattern('A'); + +=item set_pattern STRING ARRAYREF + +Add or redefine a pattern given a name and a character set. + + my $gen = String::Random->new; + $gen->set_pattern('A' => [ 'A'..'Z', 'a'..'z' ]); + =item from_pattern B - for compatibility with an old version. B From 6e9b99da347540f77a4cce152117f10fa790a8cc Mon Sep 17 00:00:00 2001 From: Gene Boggs Date: Mon, 13 Sep 2021 15:20:09 -0700 Subject: [PATCH 54/56] Initial commit --- t/08_get_set.t | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 t/08_get_set.t diff --git a/t/08_get_set.t b/t/08_get_set.t new file mode 100644 index 0000000..1a7d7f3 --- /dev/null +++ b/t/08_get_set.t @@ -0,0 +1,56 @@ +use strict; +use warnings; + +use Test::More tests => 37; + +# 1: Make sure we can load the module +BEGIN { use_ok('String::Random'); } + +# 2: Make sure we can create a new object +my $foo=new String::Random; +my $bar=String::Random->new(); +ok(defined($foo) && defined($bar), "new()"); + +# 3: Empty pattern shouldn't give undef for result +ok(my @notempty=$foo->randpattern(''), "randpattern('')"); + +# Try the object method... +$foo->set_pattern('x', ['a']); +$foo->set_pattern('y', ['b']); +$foo->set_pattern('z', ['c']); + +# 4: passing a scalar, in a scalar context +my $abc=$foo->randpattern("xyz"); +is($abc, 'abc', "randpattern()"); + +# 5: passing an array, in a scalar context +my @foo=qw(x y z); +$abc=$foo->randpattern(@foo); +is($abc, 'abc', "randpattern() (scalar)"); + +# 6-8: passing an array, in an array context +my @bar=$foo->randpattern(@foo); +for (my $n=0;$n<@foo;$n++) { + is($bar[$n], $foo->get_pattern($foo[$n])->[0], "randpattern() (array) ($n)"); +} + +# 9-34: Check one of the built-in patterns to make +# sure it contains what we think it should +my @upcase=("A".."Z"); +for (my $n=0;$n<26;$n++) { + ok(defined($foo->get_pattern('C')->[$n]) && ($upcase[$n] eq $foo->get_pattern('C')->[$n]), + "pattern ($n)"); +} + +# 35: Test modifying one of the built-in patterns +$foo->set_pattern('C', ['n']); +is($foo->randpattern("C"), "n", "modify patterns"); + +# 36: Make sure we haven't clobbered anything in an existing object +isnt($bar->randpattern("C"), "n", "pollute pattern"); + +# 37: Make sure we haven't clobbered anything in a new object +my $baz=new String::Random; +ok(defined($baz) && ($baz->randpattern("C") ne "n"), "pollute new object"); + +# vi: set ai et syntax=perl: From cc73d929d044d4041e70a56b4e9e056959e0443c Mon Sep 17 00:00:00 2001 From: Gene Boggs Date: Mon, 13 Sep 2021 15:48:49 -0700 Subject: [PATCH 55/56] Fix get/set docs a bit --- lib/String/Random.pm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 2a43c74..09fc36b 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -483,14 +483,14 @@ expression" either. Return a pattern given a name. my $gen = String::Random->new; - $gen->get_pattern('A'); + $gen->get_pattern('C'); =item set_pattern STRING ARRAYREF Add or redefine a pattern given a name and a character set. my $gen = String::Random->new; - $gen->set_pattern('A' => [ 'A'..'Z', 'a'..'z' ]); + $gen->set_pattern(A => [ 'A'..'Z', 'a'..'z' ]); =item from_pattern From 2fbfa55d8feaed5adb99c30f100f5d79a1c89f38 Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Tue, 14 Sep 2021 08:16:10 +0300 Subject: [PATCH 56/56] prepare for a release --- Changes | 5 +++++ dist.ini | 2 +- lib/String/Random.pm | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Changes b/Changes index 994cb87..b6ea8a1 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,10 @@ Revision history for Perl extension String::Random: +0.32 2021-09-14 + - Add ->get_pattern and ->set_pattern. + - https://github.com/shlomif/String-Random/pull/5 + - Thanks to https://github.com/ology + 0.31 2020-10-17 - dist.ini / weaver.ini / .tidyallrc / etc. cleanup - Move to @SHLOMIF diff --git a/dist.ini b/dist.ini index 12b2aec..37e36ff 100644 --- a/dist.ini +++ b/dist.ini @@ -1,5 +1,5 @@ name = String-Random -version = 0.31 +version = 0.32 author = Shlomi Fish license = Perl_5 copyright_holder = Shlomi Fish diff --git a/lib/String/Random.pm b/lib/String/Random.pm index 09fc36b..37994f4 100644 --- a/lib/String/Random.pm +++ b/lib/String/Random.pm @@ -485,6 +485,8 @@ Return a pattern given a name. my $gen = String::Random->new; $gen->get_pattern('C'); +(Added in version 0.32.) + =item set_pattern STRING ARRAYREF Add or redefine a pattern given a name and a character set. @@ -492,6 +494,8 @@ Add or redefine a pattern given a name and a character set. my $gen = String::Random->new; $gen->set_pattern(A => [ 'A'..'Z', 'a'..'z' ]); +(Added in version 0.32.) + =item from_pattern B - for compatibility with an old version. B