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

BEGIN{ open FH1, '>', 'my.log' or die $!; *STDOUT = *FH1; }
to the top of your program would be much simpler and more efficient.
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller



In reply to Re: Filter::Handle - Deep Recursion Error by BrowserUk
in thread Filter::Handle - Deep Recursion Error by bayruds

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.