Net::HTTP::NB(3)

πŸ’– Net::HTTP::NB πŸ’–

okay bestie, so this is Net::HTTP::NB β€” the non-blocking HTTP client, and honestly? it's giving main character energy for anyone who's ever wanted to juggle a million HTTP connections without the whole thing just... sitting there. πŸ¦‹

it lives in the User Contributed Perl Documentation corner of the manual, and the version we're stanning today is version 6.24. no cap. ✨


🌸 SYNOPSIS 🌸

here's the whole vibe, verbatim, don't touch a single character:

use Net::HTTP::NB;
my $s = Net::HTTP::NB->new(Host => "www.perl.com") || die $@;
$s->write_request(GET => "/");

use IO::Select;
my $sel = IO::Select->new($s);

READ_HEADER: {
   die "Header timeout" unless $sel->can_read(10);
   my($code, $mess, %h) = $s->read_response_headers;
   redo READ_HEADER unless $code;
}

while (1) {
   die "Body timeout" unless $sel->can_read(10);
   my $buf;
   my $n = $s->read_entity_body($buf, 1024);
   last unless $n;
   print $buf;
}

like, look at her. polling with IO::Select, redoing the header read until she gets a $code, looping the body until read_entity_body gives up. chef's kiss. πŸ˜­πŸ’…


✨ DESCRIPTION ✨

so the tea is this: Net::HTTP::NB has the same interface as Net::HTTP, but it will never try multiple reads when the read_response_headers() or read_entity_body() methods are invoked. that's the whole point, bestie. 🫢

and why do we care? because that means you can multiplex multiple Net::HTTP::NB objects using select without risk blocking. no more one connection holding the whole party hostage. iconic behavior fr fr. πŸ”₯

two little behavioral notes, memorize them:

  • if read_response_headers() did not see enough data to complete the headers, it returns an empty list.
  • if read_entity_body() did not see new entity data in its read, the value -1 is returned.

that's it. that's the whole deal. lowkey elegant ngl. 🌈


πŸ‘‘ SEE ALSO πŸ‘‘

  • Net::HTTP β€” the blocking sibling, go say hi πŸŽ€

πŸ’œ AUTHOR πŸ’œ

Gisle Aas <gisle@activestate.com> β€” absolute legend, we thank you for your service 🫑🌸


This software is copyright (c) 2001 by Gisle Aas.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


no notes, it's free software, it's Perl 5 terms, it's perfect. periodt. πŸ’…

okay that's the page!! written by perl v5.42.0, dated 2025-09-01, and yeah β€” Net::HTTP::NB(3) said what she said. bye bestie, go multiplex something responsibly πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from