in reply to Appending time to each line

You can simply replace each beginning of the line with the timestamp:

$message=~ s!^!$timestamp !mg;

Replies are listed 'Best First'.
Re^2: Appending time to each line
by ajose (Acolyte) on May 21, 2013 at 10:35 UTC
    Thanks for the reply.. But how could I print date for Data::Dumper output in each line as shown in the example?

      First capture the Data::Dumper output in a string, then prepend the timestamp:

      my $message= Dumper $myvar; $message=~ s!^!$timestamp !mg; print $message;

      See the Data::Dumper $Data::Dumper::Pad configuration variable.

      --MidLifeXis

      IMHO, it would make sense to encapsulate everything in a subroutine along the lines of

      use strict; use warnings; sub tprint { my $msg = join $,//'' ,@_; my $tstamp = localtime()." "; $msg =~ s/^/$tstamp/gm; print $msg; } tprint "something\nelse\n";

      BUT to completely replicate the interface of the print function is beyond my Perl skills. I have no idea how one could now provide a file handle to print into file.