Timer(3pm)

πŸ’– Coro::Timer β€” bestie edition πŸ’–

okay so this is Coro::Timer, a User Contributed Perl Documentation page, and it's all about timers and timeouts, independent of any event loop. no event loop required, she's self-sufficient, she's iconic. 🧚✨


🌸 NAME 🌸

Coro::Timer β€” timers and timeouts, independent of any event loop. periodt. πŸ’…


✨ SYNOPSIS ✨

okay so first things first, bestie, read the comment, it's important:

# This package is mostly obsoleted by Coro::AnyEvent.

use Coro::Timer qw(timeout);
# nothing exported by default

so you can pull in timeout with qw(timeout), but heads up: nothing is exported by default. she gives you nothing for free, you have to ask. πŸ™ˆ


πŸ¦‹ DESCRIPTION πŸ¦‹

so the tea is: this package has been mostly obsoleted by Coro::AnyEvent. like, not me delivering the news, it's literally in the docs. 😭

but the one really useful function that's still left in here is timeout. she survived the purge, she's the main character. πŸ‘‘

⭐ $flag = timeout $seconds

this function will wake up the current coroutine after $seconds seconds, and it sets $flag to true. ($flag is false initially, okay, don't come for me.)

and here's the cute part: if $flag goes out of scope earlier, then nothing happens. no wake-up, no drama, she just quietly stops existing. πŸ’€

this is used by Coro itself to implement the "timed_down", "timed_wait" etc. primitives. and it gets used like this, fr fr:

sub timed_wait {
   my $timeout = Coro::Timer::timeout 60;

   while (condition false) {
      Coro::schedule; # wait until woken up or timeout
      return 0 if $timeout; # timed out
   }

   return 1; # condition satisfied
}

see how $timeout gets checked with return 0 if $timeout;? that's the timeout flag going true and the loop bailing. and if the condition gets satisfied first, return 1; and everybody's happy. 🫢

ngl this whole pattern is giving "small function, huge responsibility" and I'm obsessed. πŸŽ€

πŸ’Œ AUTHOR/SUPPORT/CONTACT πŸ’Œ


and that's the whole page, bestie, no cap. this was Timer(3pm) from the User Contributed Perl Documentation, and the footer vibes were perl v5.40.1, dated 2025-03-21. πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«ΆπŸŒˆβ­πŸ‘‘

πŸ“¦ where this page comes from