#!/usr/bin/perl use strict; use warnings; use 5.012; # Lady Aleena's # 927621 use Data::Dumper; use Date::Calc qw(:all); use Lingua::EN::Inflect qw(ORD); #local $\ = "\n"; #commenting this out puts data entry on same line as the relevant prompt; personal pref sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } say "\n\tUsage: 4 digit year, Month as name -* OR *- as month_number, 1-12, day_of_month as number.\n"; print "What year were you born? "; chomp (my $birth_year = <>); print "What month were you born? "; chomp (my $birth_month = <>); print "What day were you born? "; chomp (my $birth_day = <>); if ( $birth_month =~ /[A-Z]+/i ) { $birth_month = convert($birth_month); # NOTE 1 in head, below } my $year = (localtime)[5] + 1900; my $month = (localtime)[4] + 1; my $day = (localtime)[3]; my @birth = ($birth_year,$birth_month,$birth_day); my @today = ($year,$month,$day); # Just figuring how many birthdays you have had. my $birthdays; if ($birth_month > $month) { $birthdays = $year - $birth_year - 1; } else { if ($birth_day > $day) { $birthdays = $year - $birth_year - 1; } else { $birthdays = $year - $birth_year; } } my $days_alive = Delta_Days(@birth,@today); my $unbirthdays = ORD(commify($days_alive - $birthdays)); print "Happy $unbirthdays unbirthday!"; sub convert # named months to Date::Calc numeric months { my $word_month=$_[0]; my %months = ('jan' => 1, # NOT zero-based - Date::Calc format 'feb' => 2, 'mar' => 3, 'apr' => 4, 'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12); my ($key, $value); while (($key, $value) = each(%months)) { if ( $key =~ /$word_month/i ) { return($value) } else { next; } } } =head NOTE 1 # Conversion with the sub here is for illustration only; in practice, it's better done with '$birth_month=Decode_Month($Birth Month);' cf perl -e "use Date::Calc qw/Decode_Month/; $mon='Dec'; $mon_name = Decode_Month($mon); print '\$mon_name: ' . $mon_name;" \$mon_name: 12 =cut