in reply to Capturing warnings from Time::Piece

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.

Replies are listed 'Best First'.
Re^2: Capturing warnings from Time::Piece
by betterworld (Curate) on Feb 24, 2005 at 02:43 UTC
    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.