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

Hi guys,
how can i dup output both stdout and a log file at a time.
pls consider below sample script,

use Test::More qw(no_plan); $\="\n"; # here will be file name which will store the log o/p print "hello world"; pass("hello world"); fail("do nothing"); diag("hi frend"); use_ok("fake_module"); print "bye";
so that it will print,
hello world ok 1 - hello world not ok 2 - do nothing # Failed test 'do nothing' # at C:/test/tst.pl line 8. # hi frend not ok 3 - use fake_module; # Failed test 'use fake_module;' # at C:/test/tst.pl line 10. bye # Tried to use 'fake_module'. # Error: Can't locate fake_module.pm in @INC (@INC contains: C:/test C +:/strawberry/perl/lib C:/strawberry/perl/site/lib .) at (eval 4) line + 2. # BEGIN failed--compilation aborted at (eval 4) line 2. 1..3 # Looks like you failed 2 tests of 3.

into both stdout and log file.I tried IO::Tee, but failed to achieve my goal.I guess i would also need to redirect stderr to log file.

cheers !!

Replies are listed 'Best First'.
Re: dup issue for Test::More module
by Khen1950fx (Canon) on Sep 11, 2010 at 12:29 UTC
    Here's my solution. You'll be needing File::Tee and IPC::System::Simple. I took your script and put it on my desktop. Then I wrote another script to call your script. If you have any further questions, post them.
    #!/usr/bin/perl use strict; no warnings; use File::Tee qw(tee); use IPC::System::Simple qw(capture system); my $test = '/root/Desktop/your_dup.pl'; tee(STDOUT, '>', 'stdout.txt'); tee(STDERR, '>', 'error.log'); my $file = system("perl $test"); print STDOUT $file, "\n"; print STDERR $file, "\n"; close STDERR;
    And your_dup.pl
    use Test::More qw(no_plan); $\="\n"; # here will be file name which will store the log o/p print "hello world"; pass("hello world"); fail("do nothing"); diag("hi friend"); use_ok("fake_module"); print "bye";
      Thank you very much :)