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

Hi Monks,

I need your wisdom again.

I have this script that calls an external program and I realised (after getting malformed header error) that the external program actually output some information. I was told to use the $| = 0 / 1 to stop the malformed header error but now the output is showing on the webpage. Is there a way to ignore the external program's outputs?

Replies are listed 'Best First'.
Re: Getting rid of extra outputs
by monarch (Priest) on May 27, 2005 at 07:38 UTC
    How are you calling the external program?

    If you are calling it like this

    my $cmd = 'ls'; my $result = `$cmd`;

    ..then consider the following approach:

    my $cmd = 'ls 2>&1'; my $result = `$cmd`;

    The purpose of the 2>&1 is to take anything output to STDERR and redirect to STDOUT.

    This will be of no use if your script is on a Win32 machine.

      No I don't call it like that.

      It is a.so file that is called by a.pm module that is provided. I call the external.pm and it calls the external.so file.

      When I run it via command line I get things like buffering 10000... and then after the results will be returned. I have a feeling that when I do a use external, it makes those statements. Is there a work around w/o having to edit external.pm? I don't want to mess about with another persons script.

        Seems like you want to temporarily redirect STDOUT to somewhere else?

        You'll find lots of good stuff if you Super Search here for "redirect stdout" or similar.

        This is a VERY basic way of doing it, which might get you started:

        print "This goes to stdout\n"; { local *STDOUT; open STDOUT, '>/tmp/myoutput'; print "This gets redirected\n"; # more code here that prints stuff to stdout } print "Back to normal stdout\n";

        If you really don't need the output of your external.pm then change /tmp/myoutput to /dev/null (if you're on Unix).

        Disclaimer: I've never actually used techniques like this in real code, there may be problems/caveats etc. Searching this site should turn up more robust solutions!


        s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&