I know I can open a filehandle and redirect it to a file like so:

open(HANDLE, ">", "filename.txt");

I can also redirect it to a command like the following:

open(HANDLE, "|-", "grep hello");

What I'd like to do is direct it to a function:

open(HANDLE, "|func", f);

Where f is a function which takes one argument, a filehandle, and then can read from that filehandle as it sees fit, and write however it wants to.

I know I can emulate this by creating a new file with f(FILEHANDLE) in it, but that has some issues, for example, if f(FILEHANDLE)s behaviour sometimes changes, instead of making a function g(option1, option2) which takes parameters and returns the function f(FILEHANDLE), in the separate file case, we'd need to pass command line parameters, with all the issues of command line quoting etc.

Any way (or any library) that allows me to direct a filehandle to a subroutine? I don't mind if the library behind the scenes does a fork(), I just want to avoid the messiness of piping the data to a shell script.

有帮助吗?

解决方案

When you want code to have the interface of a variable, you want to tie code to the variable. See Tie::Handle.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top