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

Dearest Monks, my question is probably sillier than I try to be with my perl. when I use IO::Pipe to run processes that aren't actually pipes (as I understand them), I get weird behavior.

use Modern::Perl; use IO::Pipe; my $pipe = IO::Pipe->new; $pipe->reader(qw(echo foo > bar)); while (<$pipe>){ print; }

the above code prints "foo > bar", which I don't understand. I was expecting the $pipe to just close and for nothing to be printed after the process is completed. Can someone help me clean up my thinking on this?

D

Replies are listed 'Best First'.
Re: pipe help!
by blue_cowdawg (Monsignor) on Aug 25, 2011 at 15:06 UTC
        the above code prints "foo > bar", which I don't understand.

    reader (ARGS)
    
    The object is re-blessed into a sub-class of IO::Handle , and becomes a handle at the reading end of the pipe. If ARGS are given then fork is called and ARGS are passed to exec.
    
    So what is happening is you are forking off a process such that the command is "echo" and the arguments to the command are "foo", ">" and "bar." So the behavior your are describing makes perfect sense to me. Maybe you want to
    unless (fork()){ system ("echo foo > bar"); }

    Not sure why you'd open a pipe on a command that doesn't return anything to stdout to be read by a pipe... just my thought....


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

      Thanks Peter, that does make more sense. As to the why: I'm writing a program that runs many different (but similar) programs and does stuff with the output. Most of the programs (exe) print to stdout: exe <input > output so I can open a pipe on exe <input and deal with the output directly. there are some that don't print to standard out: exe input produces output files that I read in. I went in thinking I could just use the same pipe interface for everything and then read other files afterward. I ran into problems (which I still haven't nailed down) and that's how I end up with this question. D

Re: pipe help!
by Anonymous Monk on Aug 25, 2011 at 15:02 UTC

    Did you mean for some shell to interpret the '> bar' part, rather than making them all parameters to echo?

Re: pipe help!
by locked_user sundialsvc4 (Abbot) on Aug 26, 2011 at 14:10 UTC

    Do you have a good intuitive grasp of what a pipe is?   It is an inter-process communication mechanism, really, and it can show-up in the file system and can be treated as “a file” in the sense that you can read from it and write to it.   The trick is, one process can write to it while another process is reading from it, and the data passes between them with automatic buffering graciously provided by the operating system.   (They call it a pipe, but I think of it more as a flexible, inflatable hose.)

    Please excuse me if you should (quite erroneously) think that I am being condescending.   That is absolutely not my intent.