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

is there a way to use the ..... >& filename command (catching errors) through perl? when i use it in system(..) command i get an error regarding the &. putting the & between ' ' didn't help either. using the command without the & - does not catch all errors. 10x. Yifat

Replies are listed 'Best First'.
Re: unix commands
by lhoward (Vicar) on Jun 04, 2001 at 18:04 UTC
    Try IPC::Open3, it lets you open a process for writing, and reading from stdout and stderr.
    $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH, 'some cmd and args', 'optarg', ...);
Re: unix commands
by VSarkiss (Monsignor) on Jun 04, 2001 at 18:50 UTC
    Careful: the shell syntax you've written: >& filename is for C shell and its ilk. Generally, perl will use a Bourne-type shell for implementing system().

    Basically, the >& file syntax in C shells is equivalent to the Bourne shell >file 2>&1 syntax others have posted here.

    If you're at the stage where you need finer-grain control over redirection, error output, etc., than the simple facilities of system, consider making more use of Perl's own built-ins. You can search the CPAN for process management tools, (which are usually platform-specific, such as Win32::Process or OS2::Process), or just write your own fork/exec.

Re: unix commands
by converter (Priest) on Jun 04, 2001 at 18:08 UTC

    If you want to direct the output of both stdout and stderr to the same file, use:

    system "command > output.txt 2>&1";

    "2>&1" basically means "redirect filehandle 2 (stderr) to the same place as filehandle 1 (stdout)".

Re: unix commands
by BigJoe (Curate) on Jun 04, 2001 at 17:58 UTC
    I am not sure using the system command but when I use backticks I use 2>filename to do this.
    my $testpage = `/var/www/html/wvWare --config=/var/www/html/wvHtml.xml + 2>/dev/null /tmp/abc.tmp`;
    (I use this to convert word docs to HTML on my Linux box.)
    2> takes care of the errors. Maybe it will work on the system() command.

    --BigJoe

    Learn patience, you must.
    Young PerlMonk, craves Not these things.
    Use the source Luke.
Re: unix commands
by virtualsue (Vicar) on Jun 04, 2001 at 19:32 UTC
    Hi Yifat,

    >& is not valid bourne shell syntax (looks like csh to me), and /bin/sh -c (bourne shell) is what Perl uses in order to execute the  system command string on unix systems.

    See this for the full scoop on system.

    In /bin/sh redirection works like so:

    ls > ls.out -- Send stdout of ls to ls.out mycmd > mycmd.out 2> mycmd.err -- Send stdout to mycmd.out, stderr to +mycmd.err If you'd like stdout and stderr to go to the same location, use mycmd > mycmd.log 2>&1
    It can get a lot fancier than that, see your local system documentation (man sh) or look here http://www.torget.se/users/d/Devlin/shell/man_sh.html.

Re: unix commands
by Cirollo (Friar) on Jun 04, 2001 at 18:57 UTC
    I think your problem might be caused by shell semantics; command >& file works in most cases but is not preferred. Try using &> instead, which according to the bash manpage is semantically equivalent to >word 2>&1

    Update:
    I shouldn't go off and do work in the middle of writing a reply, or two other people will beat me to it ;)