• Using circular references

    From Rainer Weikusat@21:1/5 to All on Tue Aug 17 15:32:05 2021
    Something I've been doing quite a bit recently:

    ,----
    | sub query
    | {
    | my $done = $_[0];
    | my ($hndlr, $sk, $path);
    |
    | $sk = CaMgmt::CfgListener->new(SK);
    |
    | $hndlr = $muxer->add('hnet_reply', $sk, EPOLLIN,
    | sub {
    | $hndlr = undef;
    | handle_hnet_reply($sk, $done);
    | });
    `----

    The return value of this method is an object whose destructor will
    deregister the event handler. The passed subroutine will be invoked to
    handle the event. As it references $hndlr, the event will stay
    registered until the even handling routine clears it[*], ie, due to the circular reference, it's entirely self-sufficient wrt its
    lifecycle-management.

    [*] Slightly more complicated, actually: The subroutine invoking the
    event handling routine will also hold a reference to the handler object
    until the event handling function returned.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Rainer Weikusat on Tue Aug 17 15:55:47 2021
    Rainer Weikusat <rweikusat@talktalk.net> writes:

    Something I've been doing quite a bit recently:

    ,----
    | sub query
    | {
    | my $done = $_[0];
    | my ($hndlr, $sk, $path);
    |
    | $sk = CaMgmt::CfgListener->new(SK);
    |
    | $hndlr = $muxer->add('hnet_reply', $sk, EPOLLIN,
    | sub {
    | $hndlr = undef;
    | handle_hnet_reply($sk, $done);
    | });
    `----

    The return value of this method is an object whose destructor will
    deregister the event handler. The passed subroutine will be invoked to
    handle the event. As it references $hndlr, the event will stay
    registered until the even handling routine clears it[*], ie, due to the circular reference, it's entirely self-sufficient wrt its lifecycle-management.

    Interesting technique.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)