PerlIO...imeout(3pm)
β¨ NAME β¨
PerlIO::via::Timeout β bestie, it's a PerlIO layer that adds read & write timeout to a handle π
It lives in the PerlIO::via::Timeout package (User Contributed Perl Documentation, if you're keeping track π).
πΈ VERSION πΈ
version 0.32 β that's the one, no cap.
π SYNOPSIS π
use Errno qw(ETIMEDOUT);
use PerlIO::via::Timeout qw(:all);
open my $fh, '<:via(Timeout)', 'foo.html';
# set the timeout layer to be 0.5 second read timeout
read_timeout($fh, 0.5);
my $line = <$fh>;
if ($line == undef && 0+$! == ETIMEDOUT) {
# timed out
...
}
π DESCRIPTION π
okay so this package implements a PerlIO layer, and what it does is add read / write timeout. Lowkey iconic for when you do NOT want to be stuck blocking forever while you're touching a handle (file, socket, ...), and you'd rather it just fail after some time instead of ghosting you. π«Ά
The way the timeout actually works: it uses <select> on the handle before reading/writing. That's the whole mechanism, she's simple like that. β¨
β οΈ WARNING β the handle won't timeout if you usesysreadorsyswriteon it, because these functions work at a lower level. So don't come crying to us. π BUT if you're trying to implement a timeout for a socket, go look atIO::Socket::Timeout, which implements exactly that. π
π₯ FUNCTIONS π₯
read_timeout
# set a read timeout of 2.5 seconds
read_timeout($fh, 2.5);
# get the current read timeout
my $secs = read_timeout($fh);
Getter / setter of the read timeout value. That's it, that's the function. π
write_timeout
# set a write timeout of 2.5 seconds
write_timeout($fh, 2.5);
# get the current write timeout
my $secs = write_timeout($fh);
Getter / setter of the write timeout value. Matching set, we love symmetry. π
enable_timeout
enable_timeout($fh);
Equivalent to setting timeout_enabled to 1. Turning it ON, slay. β
disable_timeout
disable_timeout($fh);
Equivalent to setting timeout_enabled to 0. Turning it OFF, bye. π
timeout_enabled
# disable timeout
timeout_enabled($fh, 0);
# enable timeout
timeout_enabled($fh, 1);
# get the current status
my $is_enabled = timeout_enabled($fh);
Getter / setter of the timeout enabled flag. The on/off switch of the whole vibe check. π
has_timeout_layer
if (has_timeout_layer($fh)) {
# set a write timeout of 2.5 seconds
write_timeout($fh, 2.5);
}
Returns whether the given filehandle is managed by PerlIO::via::Timeout. Basically: is this handle one of ours? π
π SEE ALSO π
PerlIO::via
πΈ THANKS TO πΈ
- Vincent Pit
- Christian Hansen
- Leon Timmmermans
(Yes β Timmmermans, with three m's. We copied it exactly, periodt. π)
π AUTHOR π
Damien "dams" Krotkine
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2013 by Damien "dams" Krotkine.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
and that's a wrap bestie, mwah π this was perl v5.36.0, dated 2022-10-15 β go forth and never block again π¦β¨
π¦ where this page comes from
- ubuntu noble: libperlio-via-timeout-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libperlio-via-timeout-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libperlio-via-timeout-perl 0.32-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms