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

Hi All,

I'm executing 'nmake' command from Perl program:

$status = system ("nmake -f TEST.mak CFG=\"TEST - Win32 Solaris_R +elease\"");

I need to catch the return code of 'nmake' command, check the results and send message accordantly (Success/Failed). Moreover, I want to print the outputs of the command in log file and on the screen. I've wrote the following codes to support those requirements:

open(STDOUT, "|tee -a $buildLog") || die "Can't redirect stdou +t: $buildLog";</span> open(STDERR, ">&STDOUT") || die "Can't dup stdout"; $status = system ("nmake /df $object.mak CFG=\"$object + - ${BUILD_CFG}$build_config\""); close(STDERR); close(STDOUT); if ($status) {</span> print "-E- nmake end with errors!:\n$statuss\n"; exit 1; } else { print nmake finished successfully\n"; }

Please advice if it is correct to "send" STDOUT to STDOUT or if you have other idea to implement it.

Thanks.

20080211 Janitored by Corion: Removed font tags, added code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Print STDERR&STDOUT to log file and to the screen
by cdarke (Prior) on Jan 23, 2008 at 16:43 UTC
    You could always use perl, rather than tee(1):
    use warnings; use strict; my $buildlog = 'fred.txt'; open (my $pipe, '-|', 'nmake -f TEST.mak CFG="TEST - Win32 Solaris_Release" 2>&1') || die "Unable to run nmake: $!"; open(my $log, '>', $buildlog) || die "Can't open $buildlog: $!"; while (<$pipe>) { print; print $log $_; } close ($pipe); close ($log); my $status = $?; ...
Re: Print STDERR&STDOUT to log file and to the screen
by moritz (Cardinal) on Jan 23, 2008 at 15:58 UTC
    When you execute an external command with system that command will not be affected by perl internal redirection of STDOUT.

    One possible workaround to your problem is to call tee from inside system:

    system ("nmake /df $object.mak CFG=\"$object - ${BUILD_CFG}$build_conf +ig\"|tee -a $buildLog");

    (perhaps you also want to join STDERR and STDOUT before the tee, so insert a 2>&1 in front of the pipe.

    (Update: fixed redirection thingy)

      That sulotion isn't good for me, because the system will return the result of "| tee -a $buildlog" command, and not the result of 'nmake' command. I must to get return code from 'nmake' command to check if the build success or faild!!!! Thanks.
Re: Print STDERR&STDOUT to log file and to the screen
by salva (Canon) on Jan 24, 2008 at 08:34 UTC
    There are several modules that allow you to do that (Tee).
    use File::Tee qw(tee); tee(STDOUT, '>', "foo/bar.out"); tee(STDERR, '>>', "foo/bar.err");