use Date::Calc qw/ Delta_Days check_date /; ... while( ... ){ ... my ($line_year, $line_mon, $line_day) = /(\d{4})-(\d{1,2})-(\d{1,2})/ or next # <==== this is probably desired as well, though check_date will catch (undef,undef,undef) ; if( ! check_date( $line_year,$line_mon,$line_day ) ){ warn "Bad date"; next; } my $delta_days = Delta_Days($line_year,$line_mon,$line_day,$yearto,$monthto,$dayto); ... }