STDOUT

Tim Writer tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org
Sun May 16 18:04:21 UTC 2004


Robert Brockway <robert-5LEc/6Zm6xCUd8a0hrldnti2O/JbrIOy at public.gmane.org> writes:

> On Sun, 16 May 2004, Alan Cohen wrote:
> 
> > I have a perl program that gets its input from another program's STDOUT.
> >
> > This program process STDIN and then decides what it should write to
> > STDOUT. In many cases, it will decide to write precisely what it
> > received on STDIN. It will only know for sure after it's finished
> > reading STDIN.
> >
> > Currently, I'm storing everything from STDIN to an intermediate file,
> > making my decision and then writing either a hard-coded message or the
> > intermediate file to STDOUT.
> >
> > Is there a way that doesn't require the intermediate file?
> 
> You could use a "named pipe" (FIFO) which is effectively an intermediate
> file sitting in memory rather than on disk.

Not really.  A named pipe is functionally equivalent to an ordinary pipe only
it has a name in the file system.  As a stupid example, these two shell
snippets are roughly equivalent:

    ls -1 | wc -l

    mknod pipe p
    ls -1 > pipe &
    wc -l < pipe

Typically, ordinary pipes are used to connect related processes
(i.e. processes with a common parent) whereas named pipes are used to connect
unrelated processes such as a client and a server.  A good example is
/dev/initctl.  From the init manpage:

    INTERFACE
        Init listens on a fifo in /dev, /dev/initctl, for messages.
        Telinit uses this to communicate with init.

In response to the original poster, an important question is whether you need
(or might need) to see the entire input before making your decision.  If you
need to see all of it, you may as well use a temporary file.  The logic will
probably be easier and you will be able to handle arbitrarily large files (as
long as you have sufficient tmp space).  If you only need to see a small
portion of it, you can do something like this:

    my $buf = '';
    my $pass_through = 0;
    while (<>) {
        if ($pass_through) {
            print $_;
        } else {
            $buf .= $_;
            if (seen_enough($buf)) {
                # do special stuff
                print $buf;
                $pass_through = 1;
            }
        }
    }

-- 
tim writer <tim-s/rLXaiAEBtBDgjK7y7TUQ at public.gmane.org>                                  starnix inc.
905.771.0017 ext. 225                           thornhill, ontario, canada
http://www.starnix.com              professional linux services & products
--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list