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


in reply to How to find the last date of the previous month?

In the spirit of TIMTOWTDI, here's an example using Time::Piece. The advantage of using Time::Piece is that as of Perl 5.10, it's bundled with Perl core. (As is its companion module Time::Seconds.)

use 5.010; use Time::Piece; use Time::Seconds qw( ONE_DAY ONE_HOUR ); sub last_day_of_last_month () { my $now = localtime; my $day = Time::Piece->new( $now - ($now->day_of_month * ONE_DAY) ); $day += ONE_HOUR * ($now->isdst <=> $day->isdst); return $day; } say last_day_of_last_month->mdy('/');

The daylight savings calculation is a bit of an annoyance. :-(

Yeah, that's right; I just did maths with the spaceship operator! :-)

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'