From 335f1ddbb7e48e9e781815abc36bc53c518217d0 Mon Sep 17 00:00:00 2001 From: Dave Cross Date: Sat, 8 Jun 2013 21:47:56 +0100 Subject: [PATCH] Modularise --- Lotto.pm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lotto | 62 +++------------------------------------------ 2 files changed, 80 insertions(+), 58 deletions(-) create mode 100644 Lotto.pm diff --git a/Lotto.pm b/Lotto.pm new file mode 100644 index 0000000..fca7a32 --- /dev/null +++ b/Lotto.pm @@ -0,0 +1,76 @@ +package Lotto; + +use strict; +use warnings; +use 5.010; + +require Exporter; +our @ISA = qw[Exporter]; +our @EXPORT = qw[lotto parse_config parse_args]; + +sub lotto { + my $lotto = shift; + + 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 ]; + } + + return @nums; +} + +sub parse_config { + my $config; + + while () { + chomp; + my @conf = split /:/; + my $key = shift @conf; + foreach my $def (@conf) { + my ($count, $limit) = split /x/, $def; + push @{$config->{$key}}, { limit => $limit, count => $count }; + } + } + + return $config; +} + +sub parse_args { + my $config = shift; + my ($type, $count) = qw[lotto 1]; + my @errs; + + if (@_ == 2) { + $type = shift; + if (! exists $config->{$type}) { + push @errs, qq["$type" is not a recognised type of lottery]; + } + } + + if (@_ == 1) { + $count = shift; + if ($count !~ /^\d+$/) { + push @errs, qq["$count" doesn't look like a positive integer]; + } +} + + if (@_ || @errs) { + push @errs, 'Usage: lotto [' . + join('|', keys %$config) . + "] [count]\n"; + die join "\n", @errs; + } + + return ($type, $count); +} + +1; + +__DATA__ +euro:5x50:2x11 +lotto:6x49 +thunder:5x39:1x14 diff --git a/lotto b/lotto index 5b448df..bbe10ba 100755 --- a/lotto +++ b/lotto @@ -4,68 +4,14 @@ use strict; use warnings; use 5.010; -my $config = parse_config(); - -my ($type, $count) = qw[lotto 1]; +use Lotto; -my @errs; -if (@ARGV == 2) { - $type = shift; - if (! exists $config->{$type}) { - push @errs, qq["$type" is not a recognised type of lottery]; - } -} - -if (@ARGV == 1) { - $count = shift; - if ($count !~ /^\d+$/) { - push @errs, qq["$count" doesn't look like a positive integer]; - } -} +my $config = parse_config(); -if (@ARGV || @errs) { - push @errs, 'Usage: lotto [' . join('|', keys %$config) . "] [count]\n"; - die join "\n", @errs; -} +my ($type, $count) = parse_args($config, @ARGV); for (1 .. $count) { - local $" = ', '; my @nums = lotto($config->{$type}); + local $" = ', '; say join ' : ', map { "@$_" } @nums; } - -sub lotto { - my $lotto = shift; - - 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 ]; - } - - return @nums; -} - -sub parse_config { - my $config; - - while () { - chomp; - my @conf = split /:/; - my $key = shift @conf; - foreach my $def (@conf) { - my ($count, $limit) = split /x/, $def; - push @{$config->{$key}}, { limit => $limit, count => $count }; - } - } - - return $config; -} - -__END__ -euro:5x50:2x11 -lotto:6x49 -thunder:5x39:1x14