Skip to content

Commit

Permalink
Finally worked out how to make the module into a class.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Jan 15, 2018
1 parent dd9277f commit 8c58de6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
41 changes: 26 additions & 15 deletions Lotto.pm
Expand Up @@ -10,20 +10,27 @@ our @EXPORT = qw[lotto parse_config parse_args];

our $config;

sub lotto {
my $type = shift;
my $lotto = $config->{$type};

my @nums;
foreach my $set (@$lotto) {
my %tries;
while (keys %tries < $set->{count}) {
$tries{(int rand $set->{limit}) + 1}++;
sub play {
my $self = shift;

my $lotto = $config->{$self->{type}};

my @results;

for (1 .. $self->{count}) {
my @nums;
foreach my $set (@$lotto) {
my %tries;
while (keys %tries < $set->{count}) {
$tries{(int rand $set->{limit}) + 1}++;
}
push @nums, [ sort { $a <=> $b} keys %tries ];
}
push @nums, [ sort { $a <=> $b} keys %tries ];

push @results, \@nums;
}

return @nums;
return @results;
}

sub parse_config {
Expand All @@ -38,13 +45,14 @@ sub parse_config {
}
}

sub parse_args {
sub new {
my $class = shift;

parse_config() unless keys %$config;

my ($type, $count) = qw[lotto 1];
my @errs;

return ($type, $count) unless @_;

if (@_ == 1) {
if ($_[0] =~ /^\d+$/) {
$count = shift;
Expand Down Expand Up @@ -82,7 +90,10 @@ sub parse_args {
die join "\n", @errs;
}

return ($type, $count);
return bless {
type => $type,
count => $count,
}, $class;
}

1;
Expand Down
7 changes: 3 additions & 4 deletions lotto
Expand Up @@ -8,10 +8,9 @@ use FindBin '$Bin';
use lib $Bin;
use Lotto;

my ($type, $count) = parse_args(@ARGV);
my $lotto = Lotto->new(@ARGV);

for (1 .. $count) {
my @nums = lotto($type);
for my $line ($lotto->play) {
local $" = ', ';
say join ' : ', map { "@$_" } @nums;
say join ' : ', map { "@$_" } @$line;
}

0 comments on commit 8c58de6

Please sign in to comment.