As fletch mentioned, there is only one alarm per process. (the alarm documentation also warns that some systems may implement sleep() using alarm, but that's generally not the case on Linux) Overall, alarm makes a poor tool for anything but the most simple low-level interruptions of a blocking syscall like recv() or accept().

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.


In reply to Re: Perl alarm not going off when local websocket connection is active by NERDVANA
in thread Perl alarm not going off when local websocket connection is active by jagordon59

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.