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

I am a perl novice.I downloaded this code from this site. I am not able to set the first day of the last month to 1. set_day(1) doesn't seem to work. Is there any workaround for this?
#!/usr/bin/perl use strict; use DateTime; my $today = DateTime->today(); my $last_month = $today->subtract( months => 1 ); my $first = $last_month->clone()->set_day(1); my $last = $first->add( months => 1 )->subtract( days => 1 ); print $first; print $last;
-- Ace28

Replies are listed 'Best First'.
Re: set the first day of last month to 1
by pc88mxer (Vicar) on Jul 16, 2008 at 14:54 UTC
    You need to clone() $first:
    ... my $last = $first->clone()->add( months => 1 )->subtract( days => 1 ) +;
    Otherwise $first and $last will be the same object.