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

I'm in need of a Perl plumber. Got a problem running Lincoln Stein's sample code on page 48 in both Linux and Windows. Neither system delivers the results promised in the book.

Windows98 (Perl 5.6.1) outputs this: Bad command or file name Writing line 1 Writing line 2 Writing line 3 Writing line 4 Writing line 5 Writing line 6 Writing line 7 Writing line 8 Writing line 9 Writing line 10 Wrote 1 lines of text While Linux (Perl 5.005) gives me this: Writing line 1 Broken pipe Stein says the script will do this: Writing line 1 Read_three got: This is line number 1 Writing line 2 Read_three got: This is line number 2 Writing line 3 Read_three got: This is line number 3 Writing line 4 Broken pipe
For the code from the book, see next page:
# First file ================================== #!/usr/bin/perl # file: write_ten.pl # Figure 2.3: Write ten lines of text to a pipe use strict; open (PIPE,"| read_three.pl") or die "Can't open pipe: $!"; select PIPE; $|=1; select STDOUT; my $count = 0; for (1..10) { warn "Writing line $_\n"; print PIPE "This is line number $_\n" and $count++; sleep 1; } close PIPE or die "Can't close pipe: $!"; print "Wrote $count lines of text\n"; # SECOND FILE =============================== #!/usr/bin/perl # file: read_three.pl # Figure 2.4: Read three lines of text from standard input use strict; for (1..3) { last unless defined (my $line = <>); warn "Read_three got: $line"; }

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop";
$nysus = $PM . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Pipe problem
by runrig (Abbot) on Jul 18, 2001 at 21:35 UTC
    On windows, assuming fork and everything else works, wouldn't you have to say:"| perl read_three.pl", etc. ??

      That is a good idea even when not on Win32. Some reasons include:

      • "." is often not in $ENV{PATH} under Unix
      • Your permissions might be wrong under Unix
      • Your file associations might be wrong under Win32
      • File associations break I/O redirection under Win32

              - tye (but my friends call me "Tye")

      This does the trick (Win2K), the strange thing is that without the pipe you don't need to call perl explicitly - as '.pl' is associated with perl.

      And of course - Windows doesn't give a damn if the pipe is broken or not and outputs:

      Writing line 1 Read_three got: This is line number 1 Writing line 2 Read_three got: This is line number 2 Writing line 3 Read_three got: This is line number 3 Writing line 4 Writing line 5 Writing line 6 Writing line 7 Writing line 8 Writing line 9 Writing line 10 Wrote 3 lines of text

      -- Hofmator

      OK, tried that, but I still don't get what Stein says I should get. Also, any idea on why Linux doesn't work? Here's what I get on Windows 98 with your suggestion:
      Writing line 1 Read_three got: This is line number 1 Writing line 2 Read_three got: This is line number 2 Writing line 3 Read_three got: This is line number 3 Writing line 4 Writing line 5 Writing line 6 Writing line 7 Writing line 8 Writing line 9 Writing line 10 Wrote 3 lines of text

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks

Re: Pipe problem
by RhetTbull (Curate) on Jul 18, 2001 at 22:04 UTC
    I got it to work fine on both windows (active state build 623 v5.6.0) and unix (v5.6.1). On unix, I changed the line in write_ten.pl:
    open (PIPE,"| read_three.pl") or die "Can't open pipe: $!"; to open (PIPE,"| ./read_three.pl") or die "Can't open pipe: $!";
    since on my system the current directory "." is not in my path. On windows I had to change it to:
    open (PIPE,"| perl read_three.pl") or die "Can't open pipe: $!";
    However, the output on windows is slightly different than on unix. It seems that windows doesn't recognize the broken pipe after 3 lines so I got this:
    C:\temp>perl write_ten.pl Writing line 1 Read_three got: This is line number 1 Writing line 2 Read_three got: This is line number 2 Writing line 3 Read_three got: This is line number 3 Writing line 4 Writing line 5 Writing line 6 Writing line 7 Writing line 8 Writing line 9 Writing line 10 Wrote 3 lines of text
    So, as Cubes stated above, make sure the scripts are in your path or the correct path is stated explicitely in your open and on windows, use "perl file.pl" in your open as stated above by runrig.

    Hope this helps,
    RT

Re: Pipe problem
by Cubes (Pilgrim) on Jul 18, 2001 at 21:34 UTC
    Is 'three.pl' in your $PATH? Is it executable? Does it run properly if you run it form the command line?
      It's "read_three.pl" and yes, yes, and yes. Thanks.

      $PM = "Perl Monk's";
      $MCF = "Most Clueless Friar Abbot Bishop";
      $nysus = $PM . $MCF;
      Click here if you love Perl Monks