Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Pipe Question

by Bliss (Novice)
on Apr 26, 2001 at 00:14 UTC ( [id://75601]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to pipe the STDOUT from one Perl program into another for counting and other manipulation. I am having all kinds of problems. Help is appreciated.

[--- piper.pl ---] open (P, "| from.pl") while (<P>) { print $_; $count++; } close (P); print "$count\n"; [--- from.pl ---] print "hello\n"; print "there,\n"; print "fellow\n"; print "monks\n";

My intended output from this example would be:

hello
there
fellow
monks
4

Replies are listed 'Best First'.
Re: Pipe Question
by sachmet (Scribe) on Apr 26, 2001 at 00:25 UTC
    Two notes:
    1. Your pipe is on the wrong side. It should be:
      open(P,'./from.pl|'); while (<P>) { print; $count++; } close(P); print "$count\n";
      which gives you what you expect.
    2. Make sure your scripts are mode +x! If you can't run them from the command line, they won't work.
    In general, though, remember - put the pipe on the side as you would if you ran them on the command line; that is: ./from.pl | ./piper.pl would be what you'd run in this case, so use the pipe after the script name. (If you wanted to write to it, you'd put the pipe before.)
Re: Pipe Question
by chipmunk (Parson) on Apr 26, 2001 at 00:25 UTC
    You've got the program at the wrong end of the pipe. You opened a pipe for inputting to from.pl. It's also not clear whether from.pl has the proper #! line and execute permissions.

    Try this instead:

    open(P, "perl from.pl |") or die "Can't pipe from from.pl: $?\n"; while (<P>) { print $_; $count++; } close(P) or die "Can't pipe from from.pl: $?\n"; print "$count\n";
Got it.
by Bliss (Novice) on Apr 26, 2001 at 00:53 UTC
    (highest thanks)
    two errors uncovered:

    I had tried the pipe on the right earlier, but the count failed. In rechecking the code (it is part of a larger program), I discover that I was resetting the value in my while statement, though I am not sure how. The while statement was:
    while (<R> && (count < 3)) { ... }

    Now having removed the count < 3 *and* switching the pipe to the right, we have a winner.

    Thank you all. I am astounded and delighted by the responsiveness by the visitors of this site. Truly, near tears to my eyes.
not the semi-colon
by Bliss (Novice) on Apr 26, 2001 at 00:17 UTC
    I know the terminator is missing on the "open", but not in my actual code.

    In this example, nothing prints but the count works.

    If I move the pipe to the right, it prints but the count fails.

    Thanks again.
Re: Pipe Question
by diarmuid (Beadle) on Apr 26, 2001 at 15:36 UTC
    I think what you need are either sockets perldoc IO::Socket or pr named pipes mkfifo. eg
    From the perl cookbook :
    #Shell : % mkfifo /path/to/named.pipe #Reader: open(FIFO, "< /path/to/named.pipe") or die $!; while (<FIFO>) { print "Got: $_"; } close(FIFO); #Writer: open(FIFO, "> /path/to/named.pipe") or die $1; print FIFO "Smoke this.\n"; close(FIFO);
Re: Pipe Question
by sutch (Curate) on Apr 26, 2001 at 00:23 UTC
    On my Unix system, scripts are not executable from the current directory without preappending "./" to the filename.

    Your scripts works fine when I replace your open statement with:

    open (P, "| ./from.pl");
      It shouldn't, unless you're writing to P. See my response below.

      What you're probably seeing is from.pl writing to stdout, and you're not getting a count of 4 underneath the words.
        You're correct about the placement of the pipe (and not getting a count without its correct placement).

        But on my system this works:

        open (P, "./from.pl |");

        and this does not work:

        open (P, "from.pl |");

        From the shell I must always specify a script's path, unless the path is defined in PATH.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://75601]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 23:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found