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

I've noticed that I can't chomp as I read from tied filehandles. perltie says tied filehandles are "partially implemented" so I'm not sure if this is a bug with perl or my code. I noticed the problem using IO::Socket::SSL, here's a smaller test case:
package Foo; sub TIEHANDLE { $obj = {}; bless $obj, $_[0]; } sub READLINE { return "foobar\n"; } package Main; use Foo; tie(*test, Foo); chomp($data = <test>); print join(", ", map { ord($_) } split(//, $data)) . "\n"; chomp($data); print join(", ", map { ord($_) } split(//, $data)) . "\n";

Replies are listed 'Best First'.
Re: Tied filehandles and chomp
by japhy (Canon) on Jul 12, 2002 at 15:06 UTC
    I've had this problem too. I sent a bug report, but it seems to have fallen on deaf ears.

    Perl's tie() interface is pretty bad. Perl 6 will probably fix the badness by making things METHODS, not functions. That way, you merely override the method, and there's no internal code deciding how to deal with tied variables.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Tied filehandles and chomp
by Courage (Parson) on Jul 12, 2002 at 15:26 UTC
    This seems like an interesting bug to me, both 5.6.1 and 5.8.0-pre affected. Look (I've condensed your test case a bit for use it from a single file):
    package Foo; sub TIEHANDLE { bless {}, $_[0]} sub READLINE {return "foobar\n"} package main; #use Foo; tie(*TEST, Foo); chomp($data = <TEST>); print join(", ", map { ord($_) } split(//, $data)) . "\n"; chomp($data); print join(", ", map { ord($_) } split(//, $data)) . "\n";
    results in strange output
    102, 111, 111, 98, 97, 114, 10 102, 111, 111, 98, 97, 114
    but following:
    package Foo; sub TIEHANDLE { bless {}, $_[0]} sub READLINE {return "foobar\n"} package main; #use Foo; tie(*TEST, Foo); chomp($data = <TEST>, $data); print join(", ", map { ord($_) } split(//, $data)) . "\n"; chomp($data); print join(", ", map { ord($_) } split(//, $data)) . "\n";
    and even
    package Foo; sub TIEHANDLE { bless {}, $_[0]} sub READLINE {return "foobar\n"} package main; #use Foo; tie(*TEST, Foo); chomp($data ||= <TEST>); print join(", ", map { ord($_) } split(//, $data)) . "\n"; chomp($data); print join(", ", map { ord($_) } split(//, $data)) . "\n";
    produce expected output:
    102, 111, 111, 98, 97, 114 102, 111, 111, 98, 97, 114
    My intuition says to me that it's not because of implementation of tied filehandles is incomplete, but something with a deeper level of strangeness.
    Hmm... I think I'll post this to perl5-porters list.

    Courage, the Cowardly Dog.

Re: Tied filehandles and chomp
by rsteinke (Scribe) on Jul 13, 2002 at 04:51 UTC

    Could ($data = <test>) be returning a string which is a copy of $data, instead of a reference to $data? That would explain what's happening; the first chomp() cleans up the copy, but leaves $data unaffected.

    Ron Steinke rsteinke@w-link.net
Re: Tied filehandles and chomp
by Courage (Parson) on Jul 14, 2002 at 15:56 UTC
    Good news on this, folks!

    I've reported this via perlbug system, and it was less than a day that takes to recognize and even fix this bug!

    Thanks to Hugo van der Sanden!

    Courage, the Cowardly Dog