in reply to Perl alarm not going off when local websocket connection is active
If you wrote it with websockets, I would assume you are using some sort of event library? If so, use a timer from your event library. If you are not using an event library, you should let us know a little more about the structure of the program, but we'll probably recommend something like using a select() timeout on your main loop and then checking a global list of timestamped things to see if one of them needs dealt with.
Another warning based on that snippet you shared, perl does not have convenient garbage collection like javascript; it uses reference counting and you need to ensure that you don't create reference loops. You have to be careful which variables you use inside callbacks. In that example, it appears that you have $conn referencing a disconnect callback which closes over the variable $conn, so conn refers to a coderef and the coderef refers to conn, forming a loop. Likewise, $self refers to $conn and $conn refers to a coderef and the coderef refers to $self. This means $conn (and $self) will never get garbage collected unless you do one of:
In short, for automatic garbage collection in Perl you need to imagine a tree of ownership of objects in your program, and any time you want an owned object to refer back to its owner, that needs to be a weak reference.
I've found that Mojo::IOLoop has the nicest object API to help you declare trees of event-driven objects without encouraging you to accidentally form circular references. AnyEvent is a more minimalist API, and I like minimalism, but everything is anonymous subs and it's a minefield of circular reference opportunities.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl alarm not going off when local websocket connection is active
by ikegami (Patriarch) on Apr 30, 2025 at 11:06 UTC |