Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to build a list with all the name of the months of the year using "Time::Piece". I also noticed in the docs this "Time::Piece::month_names(@months);", any idea how to use that to get the name of all the months? Trying with this, but its not working:
#!/usr/bin/perl use strict; use warnings; use Time::Piece; use Time::Piece::month_names(@months); my $t = Time::Piece->new(); my $mon = $t->mon; my $month = $t->monthname; my $year = $t->year; my @all_months = map sprintf( "%s", $_, $month ), 1 .. 12; # $mon foreach my $month ( @all_months ) { print "$month\n"; }
Thanks!

Replies are listed 'Best First'.
Re: Months of the year
by kennethk (Abbot) on Mar 31, 2014 at 19:02 UTC
    The line from the docs
    Time::Piece::mon_list(@months);
    Means that if you want to change the months list (for perhaps localization or internationalization), this is the API. If you just call the method with no arguments, you get the list of months:
    my @months = Time::Piece::mon_list;
    or
    my $t = Time::Piece->new(); my @months = $t->mon_list;

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thats a lot less code, now is trying to get the full name of the months with:
      my @months = $t->fullmonth;
      Not that easy I guess. It only gives the full name of the current month.
        use Time::Piece; use feature 'say'; my $t = Time::Piece->strptime('1', '%m'); for ( 0..11 ) { say $t->add_months($_)->fullmonth }
Re: Months of the year
by toolic (Bishop) on Mar 31, 2014 at 20:04 UTC
    Copied from the module source code:
    my @months = qw(January February March April May June July August September October November December);