In my script I need to make a cycle like this one:
use DateTime;
for $j(0..3){
my ($date) = DateTime->now->ymd;
my ($k) = 0;
while($k <= $j){
$date = ($date->subtract( days => 7));
$k++;
}
print "$date\n";
}
which should get the current date, then one week ago, etc. Sadly, after getting the correct current date, it doesn’t work and I don’t know what’s wrong.
Error message is "Can't call method "subtract" without a package or object reference [...]",
but I have no idea how to fix this.
If possible, I’d like to keep using DateTime only OR replacing it with another module (possibly no more than one).
Datetime->now->ymdis a scalar (string, it appears), not an object/reference. You can’t callsubtracton it because it doesn’t exist. You’ll probably just want to try omitting theymdpart when you assign to$date:If you want to access the
ymdvalue, do it after you’ve created the object:See the CPAN page for more.