Skip Menu |

This queue is for tickets about the random CPAN distribution.

Report information
The Basics
Id: 52331
Status: new
Priority: 0/
Queue: random

People
Owner: Nobody in particular
Requestors: mleblanc [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: Feature request
Could you change the interface such that the following behavior is exhibited: use random; rand behaves as normal. use random "integer"; rand returns an integer value. use random qw(fixed 42); rand returns 42 regardless of parameters and number of times called. no random; rand returns 0 regardless of parameters and number of times called. no random "42"; rand returns 42 regardless of parameters and number of times called. I think the addition of an unimport to cause rand to return a constant value would make the use statements clearer. In light of this, having "use random" makes sense to cause rand to return to normal behavior. Please see the attached version of random.pm which implements the behavior as described here as well as rand.pl, a demo script. random.pm also contains a fix so it no longer considers -123456789 to be a special value.
Subject: rand.pl
#!/usr/bin/perl -l use random "integer"; print qq~use random "integer";~; print rand 6 for 1..3; no random "6.82"; print $/,qq~no random "6.82";~; print rand 6 for 1..3; use random; print $/,qq~use random;~; print rand 6 for 1..3; use random qw(fixed 42); print $/,qq~use random qw(fixed 42);~; print rand 6 for 1..3;
Subject: random.pm
package random; use warnings; use strict; use 5.010; our $VERSION = '0.3'; sub import { shift; if (@_ && $_[0] eq "fixed") { $^H{random} = $_[1] + 0; } elsif (@_ && $_[0] eq "integer") { undef $^H{random} = undef; } else { delete $^H{random}; } return } sub unimport { shift; $^H{random} = (shift // 0) + 0; return; } sub rand { my $ctrl_h = ( caller 0 )[10]; my $param = $_[0] // 1; if ( ! exists $ctrl_h->{random}) { return CORE::rand($param); } elsif (! defined $ctrl_h->{random}) { return int(CORE::rand($param)); } else { return $ctrl_h->{random}; } } BEGIN { *CORE::GLOBAL::rand = *random::rand; } 1;