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

Hi, I'm new to Perl, i have a script like this :
#!/usr/bin/perl -w use strict; my @word = qw(xxx yyy zzz); open(PF,"|grep xxx") or die "failed \n"; foreach(@word) { print "here \n"; print PF $_; } print "going to close filehandle\n"; close(PF) or die "failed\n"; print "here 2\n";

I thought i'll get 'xxx' but that's not what i've got. Then i changed print PF $_; to print PF "$_ \n"; and i got the result that i expect.

But i can't explain the real different of these 2 codes to my self.

And i need your help.

It's looks when close(PF) eq to echo xxxyyyzzz |grep xxx.

Thank you

Replies are listed 'Best First'.
Re: Pipe, when this shell command is going to executed ?
by Skeeve (Parson) on Sep 08, 2006 at 20:51 UTC
    You don't output the words in your first version line by line but in one line. In your second you append one line-end to each line and so you get three lines.

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: Pipe, when this shell command is going to executed ?
by derby (Abbot) on Sep 09, 2006 at 10:50 UTC

    Well the structure is a bit odd to begin with but basically grep (and most other Unix utilities) is line-oriented. So in your first version, grep sees the input as "xxxyyyzzz" and in the second one, grep sees the input as "xxx\nyyy\nzzz\n". So the first one is similar to echo -e "xxxyyyzzz" | grep "xxx" and the second one is similar to echo -e "xxx\nyyy\nzzz" | grep "xxx"

    -derby