in reply to formatting STDOUT

select STDOUT; $|=1; unless (open STDOUT, '|-') { eval { while(<>) { print scalar(localtime), $_; } }; exit(0); }
it's not the most efficient aproach, but it's bullet proof. It will catch any output, generated from perl, from C libraries or even from external commands called via system.

Replies are listed 'Best First'.
Re^2: formatting STDOUT
by Anonymous Monk on May 31, 2005 at 14:31 UTC
    Spot on. Thanks Salva!
Re^2: formatting STDOUT
by zentara (Cardinal) on May 31, 2005 at 19:15 UTC
    Hi, this code looks interesting, but I can't seem to figure out how to make it work. Could you please post a simple working example using it? Thanks.

    I'm not really a human, but I play one on earth. flash japh
      well, it's very simple to use, after it is called on a perl script, every line written to STDOUT will get the datestamp prepended.

      for instance, create a module like:

      package MyApp::Logger; select STDOUT; $|=1; unless (open STDOUT, '|-') { eval { while(<>) { print scalar(localtime), ' ', $_; } }; exit(0); } 1;
      and then from your script, just use the module, and then print whatever you want:
      #!/usr/bin/perl use MyApp::Logger; print "foo\nbar\n";