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

Hello all,

I have script A which writes to the console. I now want to call script A from script B, but supress the output from script A and just evalutate the exit code.

I thought I would pass a flag to script A and then, depending on the value of the flag, use select to throw away the output. However, I don't know what filehandle I should pass to select.

Can anyone point me in the write right direction?

Thanks,

loris

Replies are listed 'Best First'.
Re: Suppressing console output
by blazar (Canon) on Sep 16, 2005 at 09:27 UTC
    How are you "calling" A? You can use qx aka backticks and just discard the output. Or else, following your logic, you can just re-open STDOUT to where you like most, e.g. an "in-memory" file (see open) or to a suitable actual file, including say /dev/null (assuming you're on *NIX).

      Hello blazar,

      Sorry for being so vague. I am using system. However, your suggestion about "in-memory" files was just what I was looking for.

      Thanks,

      loris

Re: Suppressing console output
by svenXY (Deacon) on Sep 16, 2005 at 09:33 UTC
    Hi,
    it looks like you need to partly rewrite Script A anyway, so why don't you just add a parameter like --verbose and only output if the script has been called with that parameter?

    Alternatively, you could use something like Log::Log4perl and then add a parameter which sets the loglevel to FATAL if you don't want output.
    Regards,
    svenXY

      Hello svenXY,

      You're right, I could do this, but I'm feeling lazy and don't want to change every line with a print. I'd like to somehow switch the output off globally.

      The idead of using Log::Log4perl is good, although as my scripts are quite short, it could be overkill.

      Thanks,

      loris

Re: Suppressing console output
by zentara (Cardinal) on Sep 16, 2005 at 14:22 UTC
    There are some shell syntax tricks you can try to suppress output, but the results vary from script to script.
    $r = system( "scriptA @options >/dev/null 2>&1 ");

    I'm not really a human, but I play one on earth. flash japh
Re: Suppressing console output
by Lunchy (Sexton) on Sep 16, 2005 at 18:08 UTC
    If I understand what you want do to correctly, whenever I want to do this, I just redirect the STDOUT filehandle to one of my own by assigning STDOUT's glob to a different one, this can be done like this...

    my $mySTDOUT = (); my $oldSTDOUT = (); *oldSTDOUT = *STDOUT; *STDOUT = *mySTDOUT; my $test = 'this is a test'; print "$test\n";

    After you run this, you should get NO output from that print command. So you'd want do to this in script A. Then, if you ever wanna restore STDOUT so you see it's output again, you can do this...

    *STDOUT = *oldSTDOUT;

    Hope that helps.

    -Lunchy

      The faster/easier way to do that is:

      { local *STDOUT; open STDOUT, '>/dev/null'; # do stuff that may print out. }
      Note also that you're creating $mySTDOUT and $oldSTDOUT and not actually using them anywhere. $mySTDOUT is not the same thing as *mySTDOUT!

        I knew that $mySTDOUT isnt' the same as *mySTDOUT, but I thought I needed to create something with a 'mySTDOUT' name to keep strict from barking about *mySTDOUT, but it turns it I didn't need to. :)

        As far as not using *oldSTDOUT. You'll note I mentioned that he could use *oldSTDOUT to restore STDOUT to normal later on if he wanted to. Perhaps there's another way to do this, but if so I don't know what it is. :)

        By the way, I DO like your solution and will try to remember it for the next time I need to do this, however, it does assume one is on an OS that has a /dev/null. :)

        Cheers!

        -Lunchy