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

There is a program I run in Perl like this `echo $script | $program`. It writes jpeg to external file. Is it possible to redirect the file write to STDOUT? Thanks.
  • Comment on Redirect file write from external program to STDOUT

Replies are listed 'Best First'.
Re: Redirect file write from external program to STDOUT
by tcf22 (Priest) on Jun 26, 2003 at 13:32 UTC
    We need a little more explanation, this question is sort of vague. If $script actually generates the JPEG binary code, then you could probably modify the script to write to STDOUT, although I don't really see the point.

    However, it looks like you are using $program to do some processing. In that case, if $program is compiled, and actually writes the JPEG to a filehandle that you can't change then probably not.

    I could be way off base here, but I would need a better explanation of what is going on to be sure.
      Normally you would do:

      prompt> program.exe script.file

      And the program.exe writes a jpeg file. script.file contains the instruction. `echo $script | $program` is simply another way to do it without physically a script.file.

      Thanks.
Re: Redirect file write from external program to STDOUT
by perlplexer (Hermit) on Jun 26, 2003 at 13:29 UTC
    No, it is not possible. Not without changing the code.
    Do you know what the file name is? If so, you could just read it in Perl itself and then print() to STDOUT.

    Hope this helps.

    --perlplexer
Re: Redirect file write from external program to STDOUT
by Zaxo (Archbishop) on Jun 26, 2003 at 13:57 UTC

    Look up the command line options and parameters for whatever $program may be. It's impossible for us to know, though '-o' is a fair guess.

    After Compline,
    Zaxo

      Even the people who wrote the program don't have an answer. The program was intended more for interactive use.
Re: Redirect file write from external program to STDOUT
by BrowserUk (Patriarch) on Jun 26, 2003 at 14:28 UTC

    You must know the name of the output file? So, if all else fails, make a copy of the program, open in a binary editor (or even using a perl script), and edit the string holding the output file name to /dev/tty or CON.

    If the output filename is shorter than the terminal device name on your OS, then this probably won't work.

    Don't forget to null terminate the string. And please make a copy first:)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      That's creative.
Re: Redirect file write from external program to STDOUT
by chip (Curate) on Jun 26, 2003 at 15:32 UTC
    If you're on a Unix-like system you might be able to tell the program to write to "/dev/stdout", then capture the standard output of the program with backticks as you already show.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Redirect file write from external program to STDOUT
by BrowserUk (Patriarch) on Jun 26, 2003 at 14:02 UTC

    Do you get to specify the name of the output file? Either through a configuration file or the command line?

    If so, you might be able to specify the name of the terminal device, con under WIn32 or /dev/tty? under *nix and that should give you what you want.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


      I can specify the name of the output file. I get to select the name of printer if I choose to print. "Terminal device" for STDOUT is what I can't find with the program. I'm using Win32. Thanks.

        In that case, specify the name as "CON" (without the quotes of course. Let us know if it works:)


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


Re: Redirect file write from external program to STDOUT
by nimdokk (Vicar) on Jun 26, 2003 at 14:11 UTC
    I think what we might need to see is a bit more of the code that you are working with to really get an idea as to what you are trying to do. Offhand, you might take a look at calling the program with open() or open a filehandle for your output and do a select to try to wrtie data into that filehandle.


    "Ex libris un peut de tout"