Skip to content

Commit

Permalink
Move $config out of the driver script.
Browse files Browse the repository at this point in the history
Allow single argument to either type or count.
  • Loading branch information
davorg committed May 17, 2014
1 parent 385fcc0 commit a8b9f97
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
21 changes: 12 additions & 9 deletions Lotto.pm
Expand Up @@ -8,8 +8,11 @@ require Exporter;
our @ISA = qw[Exporter];
our @EXPORT = qw[lotto parse_config parse_args];

our $config;

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

my @nums;
foreach my $set (@$lotto) {
Expand All @@ -24,8 +27,6 @@ sub lotto {
}

sub parse_config {
my $config;

while (<DATA>) {
chomp;
my @conf = split /:/;
Expand All @@ -35,12 +36,10 @@ sub parse_config {
push @{$config->{$key}}, { limit => $limit, count => $count };
}
}

return $config;
}

sub parse_args {
my $config = shift;
parse_config() unless keys %$config;
my ($type, $count) = qw[lotto 1];
my @errs;

Expand All @@ -52,9 +51,13 @@ sub parse_args {
}

if (@_ == 1) {
$count = shift;
if ($count !~ /^\d+$/) {
push @errs, qq["$count" doesn't look like a positive integer];
if ($_[0] =~ /^\d+$/) {
$count = shift;
} elsif (exists $config->{$_[0]}) {
$type = shift;
} else {
push @errs, qq["$_[0]" doesn't look like a positive integer or a ] .
qq[type of lottery];
}
}

Expand Down
6 changes: 2 additions & 4 deletions lotto
Expand Up @@ -8,12 +8,10 @@ use FindBin '$Bin';
use lib $Bin;
use Lotto;

my $config = parse_config();

my ($type, $count) = parse_args($config, @ARGV);
my ($type, $count) = parse_args(@ARGV);

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

0 comments on commit a8b9f97

Please sign in to comment.