What is the optimal/clearest way to loop between two dates in Perl? There are plenty of modules on CPAN that deal with such matter, but is there any rule of thumb for iterating between two dates?
What is the optimal/clearest way to loop between two dates in Perl? There are plenty of modules on CPAN that deal with such matter, but is there any rule of thumb for iterating between two dates?
As of 2020, another option would be to use Time::Moment that have very good performances (see the benchmarks) through a clear interface.
A reimplementation of Sobrique's answer would be:
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Moment;
# Same than 'Y-%m-%d'
my $FORMAT = '%F';
my $start = '2020-01-22';
my $end = '2020-03-11';
my $start_t = Time::Moment->from_string( $start . 'T00Z' );
my $end_t = Time::Moment->from_string( $end . 'T00Z' );
while ( $start_t <= $end_t ) {
print $start_t ->strftime( $FORMAT ), "\n";
$start_t->plus_days( 1 );
}
Time::Moment isn't a core module, but if you need some extra speed, it can help a bit compared to Time::Piece and DateTime. Plus, the interface is really easy to read. The date parsing capabilities are maybe a bit less restrictive.