Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Crazy question about writing to a text file that is actually an executable...

by tobyink (Abbot)
on Sep 07, 2012 at 13:06 UTC ( #992309=note: print w/ replies, xml ) Need Help??


in reply to Re: Crazy question about writing to a text file that is actually an executable...
in thread Crazy question about writing to a text file that is actually an executable...

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'


Comment on Re^2: Crazy question about writing to a text file that is actually an executable...
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992309]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-09-18 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (120 votes), past polls