IO::Any(3)
π
IO::Any(3) β the IO::Any manual page, bestie edition π
okay so this is the page for IO::Any, and honestly? it's giving "one module to rule them all" energy. it opens anything. files, strings, arrays, XML, JSON-looking stuff, globs β everything. let's get into it β¨
πΈ NAME πΈ
IO::Any β open anything. literally anything. no cap. π
β¨ SYNOPSIS β¨
quick little vibe check on the code, keep it exactly like this because these are the actual calls:
# NOTE commented out lines doesn't work (yet)
use IO::Any;
$fh = IO::Any->read('filename');
$fh = IO::Any->read('file://var/log/syslog');
#$fh = IO::Any->read('http://search.cpan.org/');
#$fh = IO::Any->read('-');
$fh = IO::Any->read(['folder', 'other-folder', 'filename']);
$fh = IO::Any->read('folder');
$fh = IO::Any->read("some text\nwith more lines\n");
$fh = IO::Any->read(\"some text\nwith more lines\n");
$fh = IO::Any->read('{"123":[1,2,3]}');
$fh = IO::Any->read('<root><element>abc</element></root>');
$fh = IO::Any->read(*DATA);
$fh = IO::Any->read(IO::String->new("cba"));
#$fh = IO::Any->read($object_with_toString_method);
$fh = IO::Any->write('filename');
$fh = IO::Any->write('file://var/log/syslog');
#$fh = IO::Any->write('-');
$fh = IO::Any->write(['folder', 'filename']);
#$fh = IO::Any->write('=');
my $string;
$fh = IO::Any->write(\$string);
my $content = IO::Any->slurp(['folder', 'filename']);
IO::Any->spew(['folder2', 'filename'], $content);
perl -MIO::Any -le 'print IO::Any->slurp("/etc/passwd")'
perl -MIO::Any -le 'IO::Any->spew("/tmp/timetick", time())'
π«Ά heads up bestie β the commented-out lines don't work (yet). they're future plans, not features. don't @ me.
π DESCRIPTION π
the whole point of this module is to give you read/write access to basically anything. it looks at $what and tries to guess what kind of thing you handed it, based on some rules. wanna see the examples? peep the new method Pod. wanna see how the sausage gets made? peep the new and _guess_what source code. iykyk π
there are also two extra-special methods, slurp and spew, that read/write the whole entire $what in one go. one bite, whole thing. π
π¦ MOTIVATION π¦
the goal here is to let other modules use IO::Any whenever they need to read or write data, so they don't have to care about what kind of thing you passed in. the way an argument gets described can basically be: "pass anything that IO::Any accepts as an argument" β GLOBs, IO::File, Path::Class::File, IO::AtomicFile, IO::String, pointers to scalar, and pointer to array (array elements get passed to catfile in File::Spec as portable file addressing). chef's kiss π
fun fact: this module was originally born for JSON::Util, where the encode/decode functions need to read and write data. main character origin story, honestly π
π₯ METHODS π₯
π new($what, $how, $options)
open $what in $how mode. simple as that.
here's what $what can be, and what it turns into:
'filename' => [ 'file' => 'filename' ],
'folder/filename' => [ 'file' => 'folder/filename' ],
'file:///folder/filename' => [ 'file' => '/folder/filename' ],
[ 'folder', 'filename' ] => [ 'file' => File::Spec->catfile('folder', 'filename') ],
'http://a/b/c' => [ 'http' => 'http://a/b/c' ],
'https://a/b/c' => [ 'http' => 'https://a/b/c' ],
'{"123":[1,2,3]}' => [ 'string' => '{"123":[1,2,3]}' ],
'[1,2,3]' => [ 'string' => '[1,2,3]' ],
'<xml></xml>' => [ 'string' => '<xml></xml>' ],
"a\nb\nc\n" => [ 'string' => "a\nb\nc\n" ],
*DATA => [ 'file' => *{DATA}{IO} ],
it returns a filehandle π«Ά. IO::String for 'string', IO::File for 'file'. also: 'http' is not implemented yet β so don't come for me, it's on the list π
these are the %$options options you got available:
atomicβ true/false, says whether the file operations should be done usingIO::AtomicFileorIO::FileLOCK_SHβ lock the file for shared accessLOCK_EXβ lock the file for exclusive accessLOCK_NBβ lock the file non-blocking (this one will throw an exception if the file is already locked, instead of just blocking the process β so like, it does not wait around π )
π§ _guess_what
returns ($type, $what). $type can be:
file
string
http
iostring
iofile
$what is the normalized path that you can use for IO::*. slay β¨
β read($what)
same as IO::Any->new($what, '<'); or IO::Any->new($what);. both work, pick your fave π
β write($what)
same as IO::Any->new($what, '>');
πΈ slurp($what)
returns the content of $what. easy.
ooh and if AnyEvent is loaded, it uses the event loop to read the content. fancy π
πΈ spew($what, $data, $opt)
writes $data to $what.
and same deal β if AnyEvent is loaded, it uses the event loop to write the content. π₯
π SEE ALSO π
IO::All, File::Spec, Path::Class β go give them a little visit, they're iconic too π
π AUTHOR π
Jozef Kutej, "<jkutej at cpan.org>" β the legend who made all this happen π
π«Ά CONTRIBUTORS π«Ά
the following people have contributed to the Sys::Path by committing their code, sending patches, reporting bugs, asking questions, suggesting useful advice, nitpicking, chatting on IRC or commenting on my blog (in no particular order, we don't rank our friends here):
SREZIC [...] cpan.org
Alexandr Ciornii
Gabor Szabo
Przemek WesoXek
Slaven ReziX
every single one of y'all ate π π
π BUGS π
please report any bugs or feature requests to "bug-io-any at rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Any>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. so like, you'll never be left on read π²β¨
π SUPPORT π
you can find documentation for this module with the perldoc command, bestie:
perldoc IO::Any
you can also look for information at:
- GitHub: issues
<http://github.com/jozef/IO-Any/issues>
- RT: CPAN's request tracker
<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-Any>
- AnnoCPAN: Annotated CPAN documentation
<http://annocpan.org/dist/IO-Any>
- CPAN Ratings
<http://cpanratings.perl.org/d/IO-Any>
- Search CPAN
<http://search.cpan.org/dist/IO-Any>
π COPYRIGHT & LICENSE π
Copyright 2009 Jozef Kutej, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay and that's the whole page bestie, you're officially an IO::Any girlie now π
β¨
tiny sign-off from the footer so nothing gets lost: this isperl v5.32.1, dated2022-01-21, and it's theIO::Any(3)manpage. mwah π«Ά
π¦ where this page comes from
- epel 9: perl-IO-Any 0.09-19.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms