Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

If you run the following

use Time::Piece; $time = Time::Piece->strptime('2005-02-24 23:000', "%Y-%m-%d %H:%S");

You get:

garbage at end of string in strptime: 0 at C:/Perl/site/lib/Time/Piece +.pm line 442.

The message is correct, but I don't want it to appear - I'd rather capture it in a variable. I've tried wrapping the code in an eval block, but this doesn't help. Any ideas? I suspect that I should be redirecting either STDOUT or STDERR, but I'm not quite clear how to do this as the examples I've seen redirect to a file, whereas I want to do it to a variable.

20050228 Edit by castaway: Changed title from 'Capturing output of Time::Local'

Replies are listed 'Best First'.
Re: Capturing warnings from Time::Piece
by Tanktalus (Canon) on Feb 24, 2005 at 02:03 UTC

    perldoc open talks about redirecting STDOUT/STDERR to a variable - at least in perl 5.8.

    my $err; close STDERR; open STDERR, '>', \$err; use Time::Piece; $time = Time::Piece->strptime('2005-02-24 23:000', "%Y-%m-%d %H:%S"); print "[$err]\n";
    You may want to open another filehandle to STDERR before closing STDERR so that you can redirect STDERR to the original console again later. Or maybe not.

      You may want to open another filehandle to STDERR before closing STDERR so that you can redirect STDERR to the original console again later.
      The same effect can be achieved in a simpler way:
      my $err; local *STDERR; open STDERR, '>', \$err; use Time::Piece; $time = Time::Piece->strptime('2005-02-24 23:000', "%Y-%m-%d %H:%S");
      Due to the local statement, when control leaves the surrounding block, STDERR will be restored to the old file handle which is connected to the console.
Re: Capturing warnings from Time::Piece
by sgifford (Prior) on Feb 24, 2005 at 04:11 UTC
    To just capture warnings without all of STDERR, you can use a $SIG{__WARN__} handler. Here's an example:
    #!/usr/bin/perl use warnings; use strict; warn "Before warn handler\n"; my $warnings; { local $SIG{__WARN__} = sub { $warnings .= join("",@_) }; my $var = "9nine"; warn "In warn handler\n"; print $var + 1,"\n"; warn "Towards end"; } warn "After warn handler\n"; print "Collected warnings:\n$warnings\n";
Re: Capturing warnings from Time::Piece
by ikegami (Patriarch) on Feb 24, 2005 at 05:32 UTC

    Of course, it might be easier to just remove the trailing 0...

    chop($string_to_parse);

    or if there's not always an extra digit:

    $string_to_parse =~ s/(:\d\d).$/$1/;
Re: Capturing warnings from Time::Piece
by Anonymous Monk on Feb 24, 2005 at 03:45 UTC
    Thanks for both your replies. Together they solved my problem
Re: Capturing warnings from Time::Piece
by Anonymous Monk on Jun 05, 2017 at 13:19 UTC

    Can you please give me more information about this message "garbage at end of string in strptime: 0 at C:/Perl/site/lib/Time/Piece +.pm line 442. ". Why is it normal ?? I had the same problem on a perl program.

      Because hours are normally restricted to 60 minutes.  23:000

      Cheers Rolf
      (addicted to the Perl Programming Language and ☆☆☆☆ :)
      Je suis Charlie!