Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Modularise

  • Loading branch information...
commit 335f1ddbb7e48e9e781815abc36bc53c518217d0 1 parent a064c7f
Dave Cross authored June 08, 2013

Showing 2 changed files with 80 additions and 58 deletions. Show diff stats Hide diff stats

  1. 76  Lotto.pm
  2. 62  lotto
76  Lotto.pm
... ...
@@ -0,0 +1,76 @@
  1
+package Lotto;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+require Exporter;
  8
+our @ISA = qw[Exporter];
  9
+our @EXPORT = qw[lotto parse_config parse_args];
  10
+
  11
+sub lotto {
  12
+  my $lotto = shift;
  13
+
  14
+  my @nums;
  15
+  foreach my $set (@$lotto) {
  16
+    my %tries;
  17
+    while (keys %tries < $set->{count}) {
  18
+      $tries{(int rand $set->{limit}) + 1}++;
  19
+    }
  20
+    push @nums, [ sort { $a <=> $b} keys %tries ];
  21
+  }
  22
+
  23
+  return @nums;
  24
+}
  25
+
  26
+sub parse_config {
  27
+  my $config;
  28
+
  29
+  while (<DATA>) {
  30
+    chomp;
  31
+    my @conf = split /:/;
  32
+    my $key = shift @conf;
  33
+    foreach my $def (@conf) {
  34
+      my ($count, $limit) = split /x/, $def;
  35
+      push @{$config->{$key}}, { limit => $limit, count => $count };
  36
+    }
  37
+  }
  38
+
  39
+  return $config;
  40
+}
  41
+
  42
+sub parse_args {
  43
+  my $config = shift;
  44
+  my ($type, $count) = qw[lotto 1];
  45
+  my @errs;
  46
+
  47
+  if (@_ == 2) {
  48
+    $type = shift;
  49
+    if (! exists $config->{$type}) {
  50
+      push @errs, qq["$type" is not a recognised type of lottery];
  51
+    }
  52
+  }
  53
+
  54
+  if (@_ == 1) {
  55
+    $count = shift;
  56
+    if ($count !~ /^\d+$/) {
  57
+      push @errs, qq["$count" doesn't look like a positive integer];
  58
+    }
  59
+}
  60
+
  61
+  if (@_ || @errs) {
  62
+    push @errs, 'Usage: lotto [' .
  63
+      join('|', keys %$config) .
  64
+      "] [count]\n";
  65
+    die join "\n", @errs;
  66
+  }
  67
+
  68
+  return ($type, $count);
  69
+}
  70
+
  71
+1;
  72
+
  73
+__DATA__
  74
+euro:5x50:2x11
  75
+lotto:6x49
  76
+thunder:5x39:1x14
62  lotto
@@ -4,68 +4,14 @@ use strict;
4 4
 use warnings;
5 5
 use 5.010;
6 6
 
7  
-my $config = parse_config();
8  
-
9  
-my ($type, $count) = qw[lotto 1];
  7
+use Lotto;
10 8
 
11  
-my @errs;
12  
-if (@ARGV == 2) {
13  
-  $type = shift;
14  
-  if (! exists $config->{$type}) {
15  
-    push @errs, qq["$type" is not a recognised type of lottery];
16  
-  }
17  
-}
18  
-
19  
-if (@ARGV == 1) {
20  
-  $count = shift;
21  
-  if ($count !~ /^\d+$/) {
22  
-    push @errs, qq["$count" doesn't look like a positive integer];
23  
-  }
24  
-}
  9
+my $config = parse_config();
25 10
 
26  
-if (@ARGV || @errs) {
27  
-  push @errs, 'Usage: lotto [' . join('|', keys %$config) . "] [count]\n";
28  
-  die join "\n", @errs;
29  
-}
  11
+my ($type, $count) = parse_args($config, @ARGV);
30 12
 
31 13
 for (1 .. $count) {
32  
-  local $" = ', ';
33 14
   my @nums = lotto($config->{$type});
  15
+  local $" = ', ';
34 16
   say join ' : ', map { "@$_" } @nums;
35 17
 }
36  
-
37  
-sub lotto {
38  
-  my $lotto = shift;
39  
-
40  
-  my @nums;
41  
-  foreach my $set (@$lotto) {
42  
-    my %tries;
43  
-    while (keys %tries < $set->{count}) {
44  
-      $tries{(int rand $set->{limit}) + 1}++;
45  
-    }
46  
-    push @nums, [ sort { $a <=> $b} keys %tries ];
47  
-  }
48  
-
49  
-  return @nums;
50  
-}
51  
-
52  
-sub parse_config {
53  
-  my $config;
54  
-
55  
-  while (<DATA>) {
56  
-    chomp;
57  
-    my @conf = split /:/;
58  
-    my $key = shift @conf;
59  
-    foreach my $def (@conf) {
60  
-      my ($count, $limit) = split /x/, $def;
61  
-      push @{$config->{$key}}, { limit => $limit, count => $count };
62  
-    }
63  
-  }
64  
-
65  
-  return $config;
66  
-}
67  
-
68  
-__END__
69  
-euro:5x50:2x11
70  
-lotto:6x49
71  
-thunder:5x39:1x14

0 notes on commit 335f1dd

Please sign in to comment.
Something went wrong with that request. Please try again.