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->ymd is a scalar (string, it appears), not an object/reference. You can't call subtract on it because it doesn't exist. You'll probably just want to try omitting the ymd part when you assign to $date:
my ($date) = DateTime->now;
...
for(0..$j) {
$date = ($date->subtract( days => 7));
}
...
If you want to access the ymd value, do it after you've created the object:
my ($date) = DateTime->now;
...
my ($ymd) = $date->ymd;
See the CPAN page for more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With