Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using circular references

9 views
Skip to first unread message

Rainer Weikusat

unread,
Aug 17, 2021, 10:32:11 AM8/17/21
to
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.


Ben Bacarisse

unread,
Aug 17, 2021, 10:55:52 AM8/17/21
to
Rainer Weikusat <rwei...@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.
0 new messages