in reply to New CPAN Module: Math::Random::MT::Auto
Hi. I had a lot of fun reading the source for this module. Here is some constructive criticism, ranging from the major to the pathologically pedantic.
{ # the values of the following hash are subs defined elsewhere my %dispatch = ( dev_random => \&_dev_random, dev_urandom => \&_dev_urandom, random_org => \&_random_org, hotbits => \&_hotbits, ); sub _seed { my $none = $_[ 0 ] eq 'none'; my @methods = @_ if not $none; @seed = (); my $magic_num = magic_num(); while ( @methods ) { my $method = shift @methods; my $need = $magic_num - @seed; if ( @methods && looks_like_number( $methods[0] ) ) { my $n = shift @methods; $need = $n if $n < $need; } unless ( defined $dispatch{ $method } ) { push @errors, "Unsupported seeding method: $method"; next; } push @seed, $dispatch{ $method }->( $need ); last if @seed >= $magic_num; } push @errors, 'Full seed not acquired from sources: ' . @seed if @seed < $magic_num and not $none; push @seed, time, $$; $#seed = $magic_num - 1 if @seed > $magic_num; # Seed the PRNG seed( @seed ); } }
Don't you want $auto_seed to be set to $sym !~ /no|!/ ? If so, I would refactor that to something more likeelsif ($sym =~ 'auto') { # To auto-seed or not $auto_seed = $sym =~ /no|!/; }
...although I must admit that I don't really understand the difference between importing 'noauto' and simply leaving 'auto' out of the import list altogether. If there is no difference, then I'd get rid of the 'noauto' option and change the fragment above toelsif ($sym =~ /^(no|!)?auto$) { $auto_seed = not defined $1; }
elsif ($sym eq 'auto') { # To auto-seed or not $auto_seed = 1; # default value for $auto_seed should be 0 }
{ no strict 'refs'; *{"${pkg}::$sym"} = \&$sym; }
Actually, I prefer this idiom:eval { require LWP::UserAgent; }; if ($@) { push(@errors, "Failure loading LWP::UserAgent: $@"); next; }
eval { require LWP::UserAgent } or do { push @errors, "Failure loading LWP::UserAgent: $@"; next; };
Update: In point 6, I changed a $sym =~ /^auto$/ to $sym eq 'auto'. What was I thinking?
the lowliest monk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: New CPAN Module: Math::Random::MT::Auto
by jdhedden (Deacon) on Jun 24, 2005 at 16:33 UTC |