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

Hi Perl Monks,


Forgive my ignorance as I'm a newbie, but is it possible to do the following in Perl on Unix/Linux?

open(LIST, "| uniq | sort -n");

Where one writes to this filehandle to remove duplicate entries and gets a sorted output?
In the current form the uniq doesn't happen and sort does.
Any tips on getting around this problem would be greatly appreciated.

Thanks again.

  • Comment on Is it possible to use the open( ) function with piped unix commands?
  • Download Code

Replies are listed 'Best First'.
Re: Is it possible to use the open( ) function with piped unix commands?
by Joost (Canon) on Jul 18, 2007 at 23:49 UTC
      And most sort's have a "-u" option so you don't need to use "uniq".

      On perl's side, closing the file handle flushes the data and releases the process (does a waitpid).

      On sort's side, the closed input tells sort it's done receiving data so it can proceed to sort and print it.

        Yes, but I assumed that perl would close F at end of script, flushing the buffers. Instead, without the close() statement, the program just waits.

        After some more testing, it appears that I got confused. Perl does close the pipe when you don't do it yourself, but the output is printed just after the new prompt. (which is why i got confused)