The problem is that you are using the module in the wrong way for your intended purpose. Using the Filter method is intended to allow you to filter the output on its way to the given file.
That is to say, it doesn't redirect the output, just gives you the opportunity to modify it on its way through, as is (slightly confusingly) described in the pod:
=item * Capturing Output
Normally, output is passed through your filtering function, then printed on the output filehandle that you're filtering.
Suppose that, instead ofwriting the filtered output to the filehandle, you just want to capture that filtered output. In other words, you want to store the output and not have it written to the filehandle.
A second problem, and the reason for the deep recursion, is that the syntax:
print <FH1> "$_\n"; isn't doing what you intend. <FH1> means "read from FH1".
So the line is attempting to read from FH1, and then print the results to STDOUT, but of course you are filtering STDOUT, so that line causes your filter function to be called, which reads from FH1 and prints the results to STDOUT, which...:)
To redirect the output from one filehandle to another, you should be using the tie interface...
#! perl -slw use strict; use Filter::Handle; open FH1, '>', 'my.log' or die $!; tie *STDOUT, 'Filter::Handle', \*FH1, sub{ $_[0] }; print 'Hello world!', $_ for 1 .. 10;
You can modify the anonymous sub on the tie to also filter the output, or omit the parameter to get the default filtering effect of each line being prefixed by the filename and linenumber.
If you are not intending to use the filtering facilities of Filter::Handle, and are only using it to re-direct the output from STDOUT to FH1, then adding
to the top of your program would be much simpler and more efficient.BEGIN{ open FH1, '>', 'my.log' or die $!; *STDOUT = *FH1; }
In reply to Re: Filter::Handle - Deep Recursion Error
by BrowserUk
in thread Filter::Handle - Deep Recursion Error
by bayruds
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |