in reply to time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469

I can't determine what is generating the error.

You could use eval to trap the errors

#!/usr/bin/perl use strict; use warnings; use CGI ':standard'; use CGI::Carp qw(fatalsToBrowser); use Time::Piece; # email addresses my $dir = '../../public_html/'; open CSV,'<',$dir.'email.txt' or die "CSV $!"; my @csv = <CSV>; close CSV; chomp(@csv); # build one regex my $re = join '|', map { quotemeta } sort { length $b <=> length $a } @csv; $re = qr/$re/; # seach text file my %date = (); my @error=(); my $lineno = 0; open BUYERS,'<',$dir."buyers.txt" or die "BUYERS $!"; for (<BUYERS>){ ++$lineno; if (/($re)/){ my $addr = $1; if (/(\d+\/\d+\/\d+)/){ my $date; if ( eval{ $date = Time::Piece->strptime($1, '%m/%d/%Y') ; 1} ){ push @{$date{$addr}},$date->strftime('%Y%m%d'); } else { push @error,"Error parsing '$addr' date : '$1' on line $lineno +\n"; } } } } # sort dates my @output; for my $addr (@csv){ push @output,$addr,sort @{$date{$addr} || []}; } # output print "Content-type: text/html\n\n"; print "$_\n" for @error; print "$_\n" for @output;
Updated
poj
  • Comment on Re: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
  • Download Code

Replies are listed 'Best First'.
Re^2: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
by choroba (Cardinal) on Mar 06, 2018 at 09:16 UTC
    eval{ $date = Time::Piece->strptime($1, '%m/%d/%Y') }; if ($@){

    Please don't, see Bug in eval in pre-5.14. At least, use

    if (eval { $date = Time::Piece->strptime($1, '%m/%d/%Y'); 1 }) { # ~~~~~
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      Thanks poj and choroba. Eval solved it though I wish I understood why. I first tried:

      if (eval { $date = Time::Piece->strptime($1, '%m/%d/%Y'); 1 }) {

      as poj suggested but this generated the repetitive date 19700101. I tweaked it with:

      if (eval { $date = Time::Piece->strptime($&, '%m/%d/%Y'); 1 }) {

      and it generated what I had hoped.
      I read the docs on eval but I'm still unclear on what problem it resolved.
      Thanks again

        I read the docs on eval but I'm still unclear on what problem it resolved.

        Perhaps this simplified code will illustrate?

        #!/usr/bin/env perl use strict; use warnings; use Time::Piece; for my $input ('05/05/2000', 'notavaliddate') { my $date; if (eval { $date = Time::Piece->strptime($input, '%m/%d/%Y'); 1 }) + { print "Parsed '$input' into '$date' with eval\n"; } else { print "Attempted parsing of '$input' trapped by eval - life go +es on\n"; } if ($date = Time::Piece->strptime($input, '%m/%d/%Y')) { print "Parsed '$input' into '$date' without eval\n"; } else { # Next line never executed because program dies print "Attempted parsing of '$input' trapped without eval\n"; } } print "Run completed OK\n"; # This line will not be reached given bad +inputs
        this generated the repetitive date 19700101

        Did you have capture brackets around the date ?

        if($_=~/$first/) { $_=~/(\d+\/\d+\/\d+)/; # ^ ^
        poj