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

I am calling a KSH using system() the KSH is outputting Error Messages to a file handle "--". Is there a way for me in my perl to redirect or duplicate the output of the "--" file handle to an error file I choose?

Replies are listed 'Best First'.
Re: File Redirection
by swampyankee (Parson) on Jan 08, 2007 at 18:00 UTC

    This is really a shell question, not a Perl question. Usually shell commands send error messages to stderr; in my experience "--" isn't a file handle, but a flag indicating that there are no more arguments to process.

    You've got a couple choices here: the easiest is use the shell's redirection operators within the command passed to system. Others would be to modify the program you're calling via system to put stderr in a specific file, or to figure out how to connect the output to stderr to a Perl filehandle. I don't even know if that's possible.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.

      I'd have to agree with swampyankee. This a weird use of --. I've only seen it in the 'no more arguments to process' mode.

      -derby
Re: File Redirection
by shmem (Chancellor) on Jan 08, 2007 at 17:57 UTC

    The shell that's spawned via system inherits perl's file handles STDIN, STDOUT and STDERR, but it is free to close and reopen them.

    You can redirect all file handles via the standard shell redirectors, e.g.

    system "script.ksh <inputfile 1>outputfile 2>errorfile";

    but not back into the parent (perl) process, since it has no control over the filehandles, instead waits for the child to exit. You'll need open for reading/writing one of STDIN/STDOUT, IPC::Open2 for input and output, and IPC::Open3 for STDIN,STDOUT and STDERR control from within perl.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: File Redirection
by jettero (Monsignor) on Jan 08, 2007 at 17:50 UTC
    You could open the other file ("--"?) from perl, not direct it to a file and use open to read from it, or pipe your ksh output through tee or something. Unless you're planing to read the output of your fork using open (instead of system), this might not even be a perl question precisely.

    An update based on the comments below... Perhaps if you included more info about your actual system call (for example what you're calling or the precise syntax you're using) we could help more. Presently I doubt the use of "--" does what you think it does.

    -Paul