in reply to Re^6: Print STDOUT and STDERR to file and to console
in thread Print STDOUT and STDERR to file and to console

Why ask There is no other simple way? Use IO::Tee if it works, seems simple :)
  • Comment on Re^7: Print STDOUT and STDERR to file and to console

Replies are listed 'Best First'.
Re^8: Print STDOUT and STDERR to file and to console
by Noame (Beadle) on Aug 02, 2009 at 11:52 UTC
    As I wrote – I can’t use external modules by definition.
    I want to print the STDOUT and STDERR to the console and log file thereat.
    Something like the code below:
    open(LOG, "> $LOG_FILE") || die "Can't redirect stdout"; open(STDERR, "> $LOG_FILE") || die "Can't dup stdout"; #open(STDERR, ">&STDOUT") || die "Can't dup stdout"; open (CMD, " ls |"); while (<CMD>) { lprint ($_) } close(CMD); sub lprint () { my ($line) = @_; print LOG $line; print STDERR $line; # print STDERR $_; print $line; }
    The problem with the code above is that I didn’t successes to control the STDERR output Please advice.
      I can’t use external modules by definition

      As ikegami was demonstrating, there is no difference whatsoever between pasting in code from one of us and pasting in code from one of us that uploaded it to the CPAN. Functionally, ethically, legally. In fact the CPAN code is safer legally because it has an explicit license. If you use code from here without specifically getting permission from the author (or from one of the members who explicitly lists the license under which she or he posts) you could theoretically run into a legal dispute. The CPAN code is probably safer functionally as it is much more likely to be tested and vetted.

      As I wrote – I can’t use external modules by definition.

      If you can use code one of us would offer, you can use external modules, even if you have to copy/paste the code.