Sha256: 295fc9a97c2e478337ceede7129bc7b00306b030de17180469a02f3611a4654b

Contents?: true

Size: 1.19 KB

Versions: 73

Compression:

Stored size: 1.19 KB

Contents

package TwelveDays;

use strict;
use warnings;

my @gifts = (
    "Partridge in a Pear Tree",
    "Turtle Doves",
    "French Hens",
    "Calling Birds",
    "Gold Rings",
    "Geese-a-Laying",
    "Swans-a-Swimming",
    "Maids-a-Milking",
    "Ladies Dancing",
    "Lords-a-Leaping",
    "Pipers Piping",
    "Drummers Drumming",
);

my @days = qw(
    first second third fourth fifth
    sixth seventh eighth ninth tenth
    eleventh twelfth
);

sub new {
    bless {} => $_[0];
}

sub verse {
    my ($self, $i) = @_;
    $i--;
    my ($nth, $nth_gifts) = ( $days[$i], _slice_gifts($i) );

    "On the $nth day of Christmas my true love gave to me, $nth_gifts.\n";
}

sub verses {
    my ($self, $start, $end) = @_;
    my @verses;

    for ($start..$end) {
       push @verses => $self->verse($_); 
    }

    join ("\n" => @verses) . "\n";
}

sub sing { $_[0]->verses(1, 12) }

sub _slice_gifts {
    my $i = shift;
    my @slice;
    my @num = qw(
        two three four five six seven
        eight nine ten eleven twelve
    );

    unshift @num => do { $i > 0 ? "and a" : "a" };

    while ($i >= 0) {
       push @slice => ($num[$i] . " " . $gifts[$i]);
       $i--;
    }

    join ", " => @slice;
}

1;

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.179 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.178 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.177 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.176 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.175 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.174 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.173 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.172 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.171 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.170 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.169 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.167 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.166 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.165 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.164 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.163 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.162 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.161 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm
trackler-2.2.1.160 tracks/perl5/exercises/twelve-days/.meta/solutions/TwelveDays.pm