On Dec 7, 2007 6:10 PM,  <[EMAIL PROTECTED]> wrote:
> I'm trying to write a subroutine that returns the full month name, for
> example January, or February when I call the subroutine and pass a
> scalar, for example $m that could have a value in one of the following
> format
>
> 1. three letter format, for example jan or feb, or
> 2. one or digit format for example "01" or "1" for january
snip

TIMTOWTDI.  In addition to the ways I implemented it below you could
use a set of cascading hashes with ||s, a substitution, and myriad*
other ways.

#!/usr/bin/perl

use strict;
use warnings;
use Date::Manip;

sub using_date_manip {
        my $month = shift or die "bad number of arguments";
        my $d = ParseDate("2007-$month-01") or die "bad argument: [$month]";
        return UnixDate($d, "%B");
}

{
        my %month_map = (
                '01' => 'January',
                  1  => 'January',
                Jan  => 'January',
                jan  => 'January',
                '02' => 'February',
                  2  => 'February',
                Feb  => 'February',
                feb  => 'February',
                etc  => 'etc'
        );
        sub using_hash {
                my $month = shift or die "bad number of arguments";
                return $month_map{$month} || die "bad argument: [$month]";
        }
}

for my $arg (qw(02 2 feb Feb)) {
        print "using_date_manip($arg): ", using_date_manip($arg), "\n",
              "using_hash($arg):       ", using_hash($arg), "\n";
}

* yes, this is hyperbole, I doubt there are 10,000 unique methods

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to