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

Please give some real-world examples of these two filehandles:
open(FH, "| myfile.txt");
open(FH, "myfile.txt |");
as I don't seem to have figured out how to make use of them to solve a real-world problem. Theoretically I understand that they set up output and input filters respectively.
  • Comment on Filehandles and Input and Output Filters

Replies are listed 'Best First'.
Re: Filehandles and Input and Output Filters
by roboticus (Chancellor) on Oct 28, 2011 at 15:40 UTC

    devarishi:

    Here's a trivial example. Suppose you have a program that generates some output, but you'd like the output sorted. You can do it like so:

    use strict; use warnings; open my $F1, '| sort >t' or die; while (<DATA>) { print $F1 $_; } __DATA__ the quick red fox jumped over the lazy brown dog

    This trivial program just writes a few words, and then uses the sort program to sort the output using the sort program as a filter. When you run it, you get this:

    $ perl foo.pl $ cat t brown dog fox jumped lazy over quick red the the

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      roboticus: Thanks a lot! Based upon my understanding of your program above, I wrote this small one:
      #!/usr/bin/perl -w open ($FH,'| sort -n') or die($!); while(<ARGV>){ print $FH $_; } close $FH;
      Thanks again for "__DATA__" as well. There is one thing I would like mention: I used to write FH instead of $FH in open() as the file-handler. But both of them work. I did not know it. Please have a look at this:
      [demo@localhost Perl]$ cat bar.pl #!/usr/bin/perl -w open (FH,'| sort') or die($!); $count=0; while(<ARGV>){ print FH $_; print $count++ . "\n"; } close FH; [demo@localhost Perl]$ bar.pl words 0 1 2 3 4 A ABC a to z Buy Why
      The line numbers are being printed out first and then the sorted list of the words. That means (this is what I understand now) that the data are being fed to the "sort" command which returns its result when all the input to it is fed.

        devarishi:

        Yes, it sounds like you've got a handle on it, now. Note that you can also have a filter on the input, too. So if you wanted to process the records in sorted order, you could open the incoming data stream with a filter to do so.

        Regarding the $FH file handle versus the FH version: Modern perl practice discourages the old style (FH) style. The older style continues to work, obviously, but using a file handle in a scalar allows you a bit more flexibility in scoping and a bit more simplicity when passing the file handle to subroutines and such.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Filehandles and Input and Output Filters
by zentara (Cardinal) on Oct 28, 2011 at 15:30 UTC
    perldoc -f open, perldoc -q filehandle, perldoc perlipc, perldoc -q capture

    Otherwise tell us what you want or need to actually do, write to the filehandle, read from it?, both? Those |'s are shortcut notation for pipes.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh