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

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

Replies are listed 'Best First'.
Re^4: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
by hippo (Archbishop) on Mar 07, 2018 at 09:01 UTC
    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
Re^4: time::piece Error parsing time at /usr/lib64/perl5/Time/Piece.pm line 469
by poj (Abbot) on Mar 07, 2018 at 11:04 UTC
    this generated the repetitive date 19700101

    Did you have capture brackets around the date ?

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