IO::Handle::Util(3pm)
π IO::Handle::Util β the IO::Handle::Util(3pm) era π
okay bestie, listen up β this is a page from the User Contributed Perl Documentation, and it's all about making stuff that acts like a filehandle without, like, being one. β¨
πΈ NAME πΈ
IO::Handle::Util β Functions for working with IO::Handle like objects. that's it, that's the whole vibe π«Ά
π SYNOPSIS π
here's the whole plot in three little scenes:
# make something that looks like a filehandle from a random data:
my $io = io_from_any $some_data;
# or from a callback that returns strings:
my $io = io_from_getline sub { return $another_line };
# create a callback that iterates through the handle
my $read_cb = io_to_read_cb $io;
likeβ¦ the range?? π she does both directions
π¦ DESCRIPTION π¦
This module provides a number of helpful routines to manipulate or create IO::Handle like objects. basically: fake it till you make it, but the faking is extremely well-engineered π
β¨ EXPORTS β¨
π Coercions resulting in IO objects
These are available using the <code>":io_from"</code> export group. (iykyk β pull them in and go)
io_from_any $whateverβ Inspects the value ofwhateverand calls the appropriate coercion function on it, eitherio_from_reforio_from_string. she's the bouncer at the door, she decides which function you're meeting tonight πio_from_ref $some_refβ Depending on the reference type of$some_refinvokes eitherio_from_object,io_from_arrayorio_from_scalar_ref.
Code references are <strong>not</strong> coerced automatically because either io_from_thunk or io_from_getline or io_from_write_cb could all make sense. like girl, we're not gonna guess which one you meant, that's rude π
Globs are returned as is only if they have a valid IO slot.
io_from_object $objβ Depending on the class of$objeither returns or coerces the object.
Objects that are passed through include anything that subclasses IO::Handle or seems to duck type (supports the print and getline methods, which might be a bit too permissive).
Objects that are coerced currently only include Path::Class::File, which will have the openr method invoked on it.
Anything else is an error. periodt π
io_from_string $strβ Instantiates anIO::Stringobject using$stras the buffer.
Note that $str is not passed as an alias, so writing to the IO object will not modify string. For that see io_from_scalar_ref. she's giving copy, not reference π
io_from_array \@arrayβ Creates anIO::Handle::Iteratorthat will return the elements of@arrayone by one.
Note that a copy of @array is made. your original array is safe, she's not touching it π«Ά
In order to be able to append more elements to the array or remove the ones that have been returned use IO::Handle::Iterator yourself directly. DIY era, honestly
io_from_scalar_ref \$strβ Creates anIO::Stringobject using$stras the buffer.
Writing to the IO object will modify $str. this one's the real one, the alias, the main character π
io_from_thunk sub { ... }β Invokes the callback once in list context the first time it's needed, and then returns each element of the list likeio_from_arraywould. lazy girl math, but make it correct β¨io_from_getline sub { ... }β Creates anIO::Handle::Iteratorobject using the callback.io_from_write_cb sub { ... }β Creates anIO::Handle::Prototype::Fallbackusing the callback.
The callback will always be invoked with one string argument and with the values of $, and $\ localized to undef. cleaning up after herself, we love to see it π§
π Coercions utilizing IO objects
These coercions will actually call <code>io_from_any</code> on their argument first. This allows you to do things like:
my $str = '';
my $sub = io_to_write_cb(\$str);
$sub->("foo");
see how the string goes in and a callback comes out?? ate π
These are available using the <code>":io_to"</code> export group.
io_to_write_cb $thingβ Creates a code ref that will invokeprinton the handle with the arguments to the callback.
$, and $\ will both be localized to undef. no stray separators sneaking in, nope π
io_to_read_cb $thingβ Creates a code ref that will invokegetlineon the handle.
$/ will not be localized and should probably be set to a reference to a number if you want efficient iteration. See perlvar for details. do your homework bestie, it's literally one variable π
io_to_string $thingβ Slurps a string out of the IO object by reading all the data.
If a string was passed it is returned as is. no extra work, no notes π
io_to_array $thingβ Returns an array reference containing all the lines of the IO object.
If an array reference was passed it is returned as is.
io_to_list $thingβ Returns the list of lines from the IO object.
Warns if not invoked in list context. she WILL call you out, respectfully π
If an array reference was passed it is dereferenced an its elements are returned.
io_to_glob $thingβ If the filehandle is an unblessed glob returns it as is, otherwise returns a new glob which is tied to delegate to the OO interface.
This lets you use most of the builtins without the method syntax: glorious π₯
my $fh = io_to_glob($some_kind_of_OO_handle);
while ( defined( my $line = <$fh> ) ) {
...
}
π Misc functions
io_prototype %callbacksβ Given a key-value pair list of named callbacks, constructs anIO::Handle::Prototype::Fallbackobject with those callbacks.
For example:
my $io = io_prototype print => sub {
my $self = shift;
no warnings 'uninitialized';
$string .= join($,, @_) . $\;
};
$io->say("Hello"); # $string now has "Hello\n"
See IO::Handle::Prototype::Fallback for more details. go read it, she's iconic too β¨
is_real_fh $ioβ Returns true if the IO handle probably could be passed to something likeAnyEvent::Handlewhich would break encapsulation.
Checks for the following conditions:
- The handle has a reftype of either a
"GLOB"with an"IO"slot, or is an"IO"itself. - The handle's
"fileno"method returns a positive number, corresponding to a filedescriptor. - The
"fileno"builtin returns the same thing as"fileno"invoked as a method.
If these conditions hold the handle is probably OK to work with using the IO builtins directly, or passing the filedescriptor to C land, instead of by invoking methods on it. vibe check passed, you're a real one π
π SEE ALSO π
IO::Handle, FileHandle, IO::String, perlio, "open" in perlfunc the whole friend group π«Ά
π§ VERSION CONTROL π§
<http://github.com/nothingmuch/io-handle-util>
go star it, bestie β
π AUTHOR π
Yuval Kogman
π COPYRIGHT & LICENSE π
Copyright (c) 2009 Yuval Kogman. All rights reserved
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
and that's the whole page, no cap π
this was <code>perl v5.36.0</code>, dated <code>2023-01-24</code>, and it lives under the manual name <code>IO::Handle::Util(3pm)</code> β slay, drink water, close your filehandles π β¨π
π¦ where this page comes from
- debian bookworm: libio-handle-util-perl 0.02-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libio-handle-util-perl 0.02-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libio-handle-util-perl 0.02-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libio-handle-util-perl 0.02-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libio-handle-util-perl 0.02-1 Β· 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