IO::Handle::Util(3pm) | User Contributed Perl Documentation | IO::Handle::Util(3pm) |
IO::Handle::Util - Functions for working with IO::Handle like objects.
# 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;
This module provides a number of helpful routines to manipulate or create IO::Handle like objects.
These are available using the ":io_from" export group.
Code references are not coerced automatically because either "io_from_thunk" or "io_from_getline" or "io_from_write_cb" could all make sense.
Globs are returned as is only if they have a valid "IO" slot.
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.
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".
Note that a copy of @array is made.
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.
Writing to the IO object will modify $str.
The callback will always be invoked with one string argument and with the values of $, and "$\" localized to "undef".
These coercions will actually call "io_from_any" on their argument first. This allows you to do things like:
my $str = ''; my $sub = io_to_write_cb(\$str); $sub->("foo");
These are available using the ":io_to" export group.
$, and "$\" will both be localized to "undef".
$/ will not be localized and should probably be set to a reference to a number if you want efficient iteration. See perlvar for details.
If a string was passed it is returned as is.
If an array reference was passed it is returned as is.
Warns if not invoked in list context.
If an array reference was passed it is dereferenced an its elements are returned.
This lets you use most of the builtins without the method syntax:
my $fh = io_to_glob($some_kind_of_OO_handle); while ( defined( my $line = <$fh> ) ) { ... }
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.
Checks for the following conditions:
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.
IO::Handle, FileHandle, IO::String, perlio, "open" in perlfunc
<http://github.com/nothingmuch/io-handle-util>
Yuval Kogman
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.
2023-01-24 | perl v5.36.0 |