Skip to content

Commit

Permalink
Add comments and better checks to BUILDARGS.
Browse files Browse the repository at this point in the history
  • Loading branch information
davorg committed Apr 26, 2018
1 parent f45634d commit b2a2224
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Lotto.pm
Expand Up @@ -68,17 +68,22 @@ around BUILDARGS => sub {

my $config = $class->config;

# Standard Moose call with hash ref. ->new({ ... })
# Just pass it on
if (@_ == 1 and ref $_[0] eq 'HASH') {
return $class->$orig(@_);
}

# Standard Moose call with has. ->new( ... )
# Take a ref and pass it on
if (@_ == 4) {
return $class->$orig(@_);
return $class->$orig({@_});
}

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

# Single arg will either be a lottery type string or an integer
if (@_ == 1) {
if ($_[0] =~ /^\d+$/) {
$count = shift;
Expand All @@ -90,6 +95,7 @@ around BUILDARGS => sub {
}
}

# Two args should be both a lottery type string and an integer
if (@_ == 2) {
($count, $type) = (undef, undef);
my ($first, $second) = (shift, shift);
Expand All @@ -109,6 +115,10 @@ around BUILDARGS => sub {
}
}

if (@_ > 2) {
die "Weird parameters to Lotto constructor: @_\n";
}

if (@_ || @errs) {
push @errs, 'Usage: lotto [' .
join('|', keys %{$class->config}) .
Expand Down

0 comments on commit b2a2224

Please sign in to comment.