To slightly clarify what BrowserUk said, what you're looking for is already a feature of most operating systems, including pretty much any UNIX-based OS you like. The feature is usually referred to as "named pipes" or "FIFOs".

You use a command mkfifo to create what is basically a dummy entry in the filesystem. You then write your script to read lines from that filename in a loop. When there are no lines to be read, then Perl's readline operator (i.e. <$fh>) will automatically wait until lines are available.

Other processes can then just open that file in write mode and write lines to it, just like it was a normal file. Your script will wake up when lines are available and be able to process them. If your script is slow to process the data then the process writing the data will just act like it's writing to a slow disk. Nothing actually gets written to disk (except the directory entry for the filename); the data streams from the output filehandle of one process to the input handle of the other process.

This is basically just the same technique as chaining together commands in the shell using the pipe (|), but rather than the pipes being anonymous, they have filenames attached to them. Hence "named pipes".

Named pipes are pretty awesome for interprocess communication, though can be fiddly to set up, so generally people use things like TCP sockets instead. The one place where named pipes beat sockets is where (as in your question) either the reader or the writer has not been written with interprocess communication in mind, so you need to "trick" it into thinking it's reading from a plain old file.

I seem to remember at one point using named pipes to include a randomly generated message in my e-mail signatures, when at the time my mail client could only read signatures from a static text file.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re^2: Crazy question about writing to a text file that is actually an executable... by tobyink
in thread Crazy question about writing to a text file that is actually an executable... by bcarroll

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.