문제

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