Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Move $config out of the driver script.

Allow single argument to either type or count.
  • Loading branch information...
commit a8b9f9767a25378e00ad2a3300897a276a3661c3 1 parent 385fcc0
@davorg authored
Showing with 14 additions and 13 deletions.
  1. +12 −9 Lotto.pm
  2. +2 −4 lotto
View
21 Lotto.pm
@@ -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) {
@@ -24,8 +27,6 @@ sub lotto {
}
sub parse_config {
- my $config;
-
while (<DATA>) {
chomp;
my @conf = split /:/;
@@ -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;
@@ -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];
}
}
View
6 lotto
@@ -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;
}
Please sign in to comment.
Something went wrong with that request. Please try again.