tima has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use Filter::Handle in a script I'm writing (it does exact what I want I need) and see that its tests have been segfaulting on Perl 5.8+. Perl 5.6 seems uneffected. The problem lies in this test for the Filter and UnFilter methods:

my $out; Filter \*STDOUT, sub { $out = sprintf "%d: %s\n", 1, "@_"; () }; print "Foo"; UnFilter \*STDOUT; print $out eq "1: Foo\n" ? "ok 3\n" : "not ok 3\n";

Here is the code to those two methods:

sub Filter { my $fh = $_[0]; tie *{ $fh }, __PACKAGE__, @_; } sub UnFilter { my $fh = shift; { local $^W = 0; untie *{ $fh } } }

Perl's internals are not my forte so I don't see or understand what is going wrong. I was hoping someone here could clue me in. The maintainer of the module has his hands full with a few other things right now.

Thanks.

Replies are listed 'Best First'.
Re: Filter::Handle segfault in Perl 5.8+
by tima (Acolyte) on Oct 26, 2007 at 22:42 UTC

    I uncovered some more information on this problem. While the test code I originally posted didn't report anythingon the segfault, this code...

    #!/usr/bin/perl -w use strict; use Filter::Handle qw/subs/; Filter \*STDOUT, sub { local $_ = "@_"; s/blue/red/g; $_ }; print "My house is blue.\n"; print "So is my cat, whose nose is blue.\n"; UnFilter \*STDOUT; print "And the plane is also blue.\n";

    ... gave me a "Deep recursion on subroutine "Filter::Handle::PRINT" at /Library/Perl/5.8.6/Filter/Handle.pm line 42." That gave me some clues, but what I thought was the solution isn't working.

    Here is the subroutine with line 42 in it.

    sub PRINT { my $self = shift; my $fh = *{ $self->{fh} }; print $fh, $self->{output}->(@_); }

    The last line in #42. Immediately I thought perl is getting confused to which print to call and going into an infinite loop until a segfault happens. Thing is prepending CORE:: still ends in the same results.

    sub PRINT { my $self = shift; my $fh = *{ $self->{fh} }; CORE::print $fh $self->{output}->(@_); }

    I also added a "use subs" for print and printf to no avail.

    use subs qw(print printf);

    My experiments with Tie::Handle have return similar results so far.

    What is happening here to confuse Perl that it goes into an infinite loop? Why is the use of CORE:: getting ignored or changed by this code?

    Any ideas would be appreciated.

      OK I finally got my head out of my backside and figured it out -- at least for Perl 5.8. Here is the patch I'll be submitting to Ben:
      41,42c41,42 < my $fh = *{ $self->{fh} }; < print $fh $self->{output}->(@_); --- > open FH, ">&", \*{ $self->{fh} }; # dup to avoid potential STDOU +T recursion > print FH $self->{output}->(@_);