IO::As...:Select(3pm)

✨ IO::Async::Loop::Select β€” bestie's guide πŸ’…

okay so this page is from the perl manual, section 3pm, and it's literally called IO::As...:Select(3pm) because the title got truncated by the terminal and nobody fixed it (iconic behaviour honestly). Real name: IO::Async::Loop::Select β€” it's the class that lets you run IO::Async on top of a plain old select(2) syscall. let's get into it πŸ¦‹


🌸 NAME 🌸

IO::Async::Loop::Select β€” use IO::Async with select(2)


✨ SYNOPSIS ✨

so normally you would not be out here constructing one of these yourself, bestie. BUT. It can genuinely be useful if you want to run IO::Async inside a program that's already doing its own select call. like, you're moving in, and select is already living there. coexisting. roommates. 🫢

use IO::Async::Loop::Select;

my $loop = IO::Async::Loop::Select->new;

$loop->add( ... );

while(1) {
   my ( $rvec, $wvec, $evec ) = ('') x 3;
   my $timeout;

   $loop->pre_select( \$rvec, \$wvec, \$evec, \$timeout );
   ...
   my $ret = select( $rvec, $wvec, $evec, $timeout );
   ...
   $loop->post_select( $rvec, $evec, $wvec );
}

πŸ’œ DESCRIPTION πŸ’œ

This is a subclass of IO::Async::Loop, and it uses the select(2) syscall to do its read-ready and write-ready tests. that's the whole job. she's simple, she's reliable, she slays. πŸ’…

Now β€” if you wanna integrate with an existing select-based event loop, there's a lil duo of methods for you: pre_select and post_select. You call them immediately before and immediately after your select call. Here's the tea:

  • pre_select sets the relevant bits in the read-ready, write-ready and exceptional-state bitvectors for you.
  • post_select then tests those bitvectors to figure out which event callbacks to actually invoke.

so it's giving "you do the select, we do the bookkeeping". no cap. πŸ“šβœ¨


πŸŽ€ CONSTRUCTOR πŸŽ€

new

$loop = IO::Async::Loop::Select->new;

This function hands you back a brand new instance of an IO::Async::Loop::Select object. It takes no special arguments β€” zero drama, zero required params, we love a low-maintenance queen πŸ‘‘


πŸ”₯ METHODS πŸ”₯

pre_select

$loop->pre_select( \$readvec, \$writevec, \$exceptvec, \$timeout );

This method gets the bitvectors all prepped and ready for a select call, setting the bits that the Loop is actually interested in. It will also adjust the $timeout value if it needs to β€” specifically, it reduces it if the next event timeout the Loop needs is sooner than whatever value is currently sitting in there. like, if something urgent is coming up, she's gonna say "bestie, wake up earlier". ⏰

  • \$readvec<br> \$writevec<br> \$exceptvec β€” scalar references to the reading, writing and exception bitvectors
  • \$timeout β€” scalar reference to the timeout value

post_select

$loop->post_select( $readvec, $writevec, $exceptvec );

This one checks the bitvectors that came back from a select call, and then calls any of the callbacks that are appropriate. it's the "so what actually happened while I was asleep" method. 😴➑️🫑

  • $readvec<br> $writevec<br> $exceptvec β€” scalars containing the read-ready, write-ready and exception bitvectors
note bestie: pre_select takes references (\$readvec) and post_select takes the actual scalars ($readvec). do not mix these up, i'm begging πŸ™ˆ

loop_once

$count = $loop->loop_once( $timeout );

This method does the whole lil routine: it calls pre_select to get the bitvectors ready for a select syscall, performs that syscall, and then calls post_select to process whatever came back.

It returns the total number of callbacks that were invoked by the post_select method. πŸŽ‰

OR β€” and this is the important part β€” it returns "undef" if the underlying select(2) syscall returned an error. so like, check your return value, fr fr. πŸ’€


🌈 SEE ALSO 🌈

  • IO::Select β€” OO interface to select system call

πŸ‘‘ AUTHOR πŸ‘‘

Paul Evans <leonerd@leonerd.org.uk>


and that's the whole page, bestie!! πŸ’– this one's from perl v5.40.1, dated 2026-02-21, so it's fresh out the group chat. go forth and select responsibly πŸ¦‹βœ¨πŸŽ€

πŸ“¦ where this page comes from