in reply to IO::Multiplex and tied handles?

After some experimenting -- no, this doesn't work. "Freezing" the write function before the tie() won't help, since the dispatch is happening later, at call time, depending of the then current ties, as can be seen in

#!/usr/bin/perl # # tying knots: how to override a tied filehandle and still access its +guts? use IO::Handle; use Data::Dumper; package Hahandle; use base qw(Tie::Handle); sub TIEHANDLE { my ($class, $ll) = @_; # ll: the "lower level ref" return bless { _ll => $ll, _llwrite => $ll->can('syswrite') }, $class; } sub WRITE { my($self, $buf, $len, $offs) = @_; $buf =~ s/(\w+)/$1 $1/g; # quite dirty syswrite(STDOUT, $buf, length($buf), $offs); } sub llwrite { my $self = shift; $self->{_llwrite}($self->{_ll}, @_); } ################ package main; my $out = IO::Handle->new_from_fd(STDOUT, "w"); $out->write("(0) bong!\n"); my $wr = $out->can('syswrite'); tie(*$out, 'Hahandle', $out); $out->write("(1) bong!\n"); &$wr($out, "(2) bong!\n");

which yields

(0) bong! (1 1) bong bong! (2 2) bong bong!

Gaah. Of course, I might do fdopen before tie()ing, but I guess I'd lose the original tie. Help. Is there a way to "stack ties"?

Replies are listed 'Best First'.
Re^2: IO::Multiplex and tied handles?
by oldtomas (Novice) on May 29, 2012 at 08:21 UTC

    There seems to exist a solution (phew)

    Handwaving

    Very lightly tested, and it might depend heavily on the details of the tied handle, but this approach already showed some "signs of life"

    The gist is, you have to stash away the tied object before re-tieing it, and then delegate to its methods when it comes to reading and writing.

    Show me the code

    In the case of IO::Multiplex and IO::Socket::SSL that means:

    Stash away the tied object

    In IO::Multiplex's add method, stash away the tied object before it gets steamrolled by the new tie()

    sub add { [...] $self->{_fhs}{"$fh"}{oldtie} = tied(*$fh) if defined(tied(*$fh)); $self->{_handles}{"$fh"} = $fh; tie *$fh, "IO::Multiplex::Handle", $self, $fh; return $fh; }

    Delegate read, write to the stashed-away object, fall back to POSIX::read and POSIX::write

    Somewhere in IO::Multiplex's loop method, replace the direct calls to POSIX::read resp. POSIX::write by those two code snippets:
    $rv = ($self->{_fhs}{"$fh"}{oldtie}) ? $self->{_fhs}{"$fh"}{oldtie}->READ($data, BUFSIZ) : &POSIX::read(fileno($fh), $data, BUFSIZ);
    resp.
    $rv = ($self->{_fhs}{"$fh"}{oldtie}) ? $self->{_fhs}{"$fh"}{oldtie}->READ($data, BUFSIZ) : &POSIX::read(fileno($fh), $data, BUFSIZ);

    Caveats

    This code is in the "I-tried-it-once-and-it-seemed-to-work" category. Use with care.

    Don't forget goggles and gloves.