use warnings;
use strict;
use Date::Calc qw/Add_Delta_YM/;
sub prevmonth {
my ($year, $month) = @_;
my ($newyear, $newmonth, undef)
= Add_Delta_YM( $year, $month, 1, 0, -1 );
return [$newyear, $newmonth];
}
use Test::More;
is_deeply prevmonth(2003,8), [2003,7];
is_deeply prevmonth(2004,1), [2003,12];
done_testing;
Update: This is also possible with the core module Time::Piece. If you have a modern browser like the latest Firefox or Chrome, here is a live demo of the following code.
use warnings;
use strict;
use Time::Piece;
while (<>) {
chomp;
my $t = Time::Piece->strptime($_, '%m/%Y');
my $prev = $t->add_months(-1);
print $prev->strftime('%m/%Y'), "\n";
}