Using circular references

6 views
Skip to first unread message

Rainer Weikusat

unread,
Aug 17, 2021, 10:32:11 AMAug 17
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 AMAug 17
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.
Reply all
Reply to author
Forward
0 new messages