Here's my refactored version of your demonstration script:
use strict;
use warnings;
use Time::Piece;
my $year = shift @ARGV || 2014;
my $p = '%Y/%m/%d'; # strptime() format
my $f = '%a %b %d %Y'; # strftime() format
my $t = Time::Piece->new();
for my $month (1 .. 12) {
# Compute the last day of the month...
my $monthLastDay
= $t->strptime("$year/$month/1", $p)->month_last_day();
# Compute the day of the week of the last day of the month (Sunday
+ = 1)...
my $weekDay
= $t->strptime("$year/$month/$monthLastDay", $p)->wday();
# Compute the day of the last Friday of the month (Friday = 6)...
my $lastFridayDate
= $monthLastDay - (($weekDay - 6) % 7);
# Compute the last Friday of the month...
my $lastFriday
= $t->strptime("$year/$month/$lastFridayDate", $p)->strftime($
+f);
print "$lastFriday\n";
}
exit 0;
__END__
Fri Jan 31 2014
Fri Feb 28 2014
Fri Mar 28 2014
Fri Apr 25 2014
Fri May 30 2014
Fri Jun 27 2014
Fri Jul 25 2014
Fri Aug 29 2014
Fri Sep 26 2014
Fri Oct 31 2014
Fri Nov 28 2014
Fri Dec 26 2014
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|