http://www.perlmonks.org?node_id=927709


in reply to Happy unbirthday!

Your perseverence wins plaudits. + +, Lady A!

FWIW, it's only a minor mod to make your program accept month_names. Wisdom says, do it with the capabilities already written into Date::Calc and tested repeatedly. Curiousity said, 'Do something different!'

Hence, this derivative - which is basicly your code with minor mods and a hash as the heart of the conversion (for all I know, this is what happens inside use Date::Calc qw/Decode_Month/ but the night's too far gone to check that):

#!/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 li +ne 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 forma +t '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 = Deco +de_Month($mon); print '\$mon_name: ' . $mon_name;" \$mon_name: 12 =cut

Now, a fairly trivial challenge -- chiefly for newcomers, please -- Add error checking on the data entered for birth_year and birth_month... and for bonus points, code or narrative description of other enhancements. The effort may put you on the path Lady_A has followed with such success.