SYNOPSIS

    # 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;

DESCRIPTION

This module provides a number of helpful routines to manipulate or create IO::Handle like objects.

EXPORTS

Coercions resulting in \s-1IO\s0 objects

These are available using the \*(C`:io_from\*(C' export group. Inspects the value of \*(C`whatever\*(C' and calls the appropriate coercion function on it, either \*(C`io_from_ref\*(C' or \*(C`io_from_string\*(C'. Depending on the reference type of $some_ref invokes either \*(C`io_from_object\*(C', \*(C`io_from_array\*(C' or \*(C`io_from_scalar_ref\*(C'. Code references are not coerced automatically because either \*(C`io_from_thunk\*(C' or \*(C`io_from_getline\*(C' or \*(C`io_from_write_cb\*(C' could all make sense. Globs are returned as is only if they have a valid \*(C`IO\*(C' slot. Depending on the class of $obj either returns or coerces the object.

Objects that are passed through include anything that subclasses IO::Handle or seems to duck type (supports the \*(C`print\*(C' and \*(C`getline\*(C' methods, which might be a bit too permissive).

Objects that are coerced currently only include Path::Class::File, which will have the \*(C`openr\*(C' method invoked on it.

Anything else is an error. Instantiates an IO::String object using $str as the buffer. Note that $str is not passed as an alias, so writing to the \s-1IO\s0 object will not modify string. For that see \*(C`io_from_scalar_ref\*(C'.

io_from_array \@array

Creates an IO::Handle::Iterator that will return the elements of @array one by one. 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.

io_from_scalar_ref \$str

Creates an IO::String object using $str as the buffer. Writing to the \s-1IO\s0 object will modify $str.

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 like \*(C`io_from_array\*(C' would.

io_from_getline sub { ... }

Creates an IO::Handle::Iterator object using the callback.

io_from_write_cb sub { ... }

Creates an IO::Handle::Prototype::Fallback using the callback. The callback will always be invoked with one string argument and with the values of $, and \*(C`$\\*(C' localized to \*(C`undef\*(C'.

Coercions utilizing \s-1IO\s0 objects

These coercions will actually call \*(C`io_from_any\*(C' 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 \*(C`:io_to\*(C' export group. Creates a code ref that will invoke \*(C`print\*(C' on the handle with the arguments to the callback. $, and \*(C`$\\*(C' will both be localized to \*(C`undef\*(C'. Creates a code ref that will invoke \*(C`getline\*(C' on 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. Slurps a string out of the \s-1IO\s0 object by reading all the data. If a string was passed it is returned as is. Returns an array reference containing all the lines of the \s-1IO\s0 object. If an array reference was passed it is returned as is. Returns the list of lines from the \s-1IO\s0 object. Warns if not invoked in list context. If an array reference was passed it is dereferenced an its elements are returned. If the filehandle is an unblessed glob returns it as is, otherwise returns a new glob which is tied to delegate to the \s-1OO\s0 interface. 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> ) ) { ... }

Misc functions

Given a key-value pair list of named callbacks, constructs an IO::Handle::Prototype::Fallback object 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. Returns true if the \s-1IO\s0 handle probably could be passed to something like AnyEvent::Handle which would break encapsulation. Checks for the following conditions:

  • The handle has a reftype of either a \*(C`GLOB\*(C' with an \*(C`IO\*(C' slot, or is an \*(C`IO\*(C' itself.

  • The handle's \*(C`fileno\*(C' method returns a positive number, corresponding to a filedescriptor.

  • The \*(C`fileno\*(C' builtin returns the same thing as \*(C`fileno\*(C' invoked as a method.

If these conditions hold the handle is probably \s-1OK\s0 to work with using the \s-1IO\s0 builtins directly, or passing the filedesctiptor to C land, instead of by invoking methods on it.

RELATED TO IO::Handle::Util…

IO::Handle, FileHandle, IO::String, perlio, \*(L"open\*(R" in perlfunc

VERSION CONTROL

<http://github.com/nothingmuch/io-handle-util>

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.