RFC: AnyEvent::Handle with two non-duplex filehandles

Marc Lehmann schmorp at schmorp.de
Tue Apr 23 18:29:25 CEST 2013


On Tue, Apr 23, 2013 at 03:09:57PM +1200, Mark Lawrence <nomad at null.net> wrote:
> > > IPC::* etc) all seem to return or require separate in/out
> > 
> > Are you sure they require separate handles? That happens when you
> > pass in the same handle for stdin and stdout?
> 
> Given that they are mostly pipe based (one-directional at least on
> Linux) I would expect this to fail, but I haven't confirmed this.

Well, try a socketpair (e.g. one created with
AnyEvent::Util::portable_socketpair).

> > The easy solution that should alwas work is have a wrapper object
> > that only points to the real object(s) - since nothing inside keeps a
> > reference to the wrapper, it's DESTROY can unravel the object
> > hierarchy. XML::Parser uses this for example.
> 
> Do you mean something like this?:

no, sth. like this:

   my $wrapper = bless { rfh => $read_handle, wfh => $write_handle };

   sub DESTROY {
      $_[0]{rfh}->destroy;
      $_[0]{wfh}->destroy;
   }

then use read_handle and write_handle to your liking, and when you decide you
are finished (when you would normall get rid of the handles), you:

   undef $wrapper;

A guard function will do just fine - the idea is to destroy "manually".

The wrapper function becomes more useufl when you hand out your objects to
some caller who shouldn't be bothered with internal details, and it's only
purpose is to not have references to itself except the ones that should
keep it alive.

> However as I see it, I have still created a ref-count issue of some
> kind here: $wrapper contains a reference to itself inside the subref,
> no?

You can also use weak references to refer to the other handle (and the
wrapper object).

Then you don't even need a destroy function, as the only non-weak
references are from the wrapper to the handles,a nd not vice versa.

> Ideally perhaps I would make an AnyEvent::Handle object the wrapper,
> and add other things to _it_? Then there is no need for variables to be
> scoped inside anonymous subrefs:

You could do that as well, AnyEvent::Handle happily accepts additional
members.

-- 
                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_              http://www.deliantra.net
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at schmorp.de
      -=====/_/_//_/\_,_/ /_/\_\



More information about the anyevent mailing list