1IO::Any(3)            User Contributed Perl Documentation           IO::Any(3)
2
3
4

NAME

6       IO::Any - open anything
7

SYNOPSIS

9           # NOTE commented out lines doesn't work (jet)
10           use IO::Any;
11
12           $fh = IO::Any->read('filename');
13           $fh = IO::Any->read('file://var/log/syslog');
14           #$fh = IO::Any->read('http://search.cpan.org/');
15           #$fh = IO::Any->read('-');
16           $fh = IO::Any->read(['folder', 'other-folder', 'filename']);
17           $fh = IO::Any->read('folder');
18           $fh = IO::Any->read("some text\nwith more lines\n");
19           $fh = IO::Any->read(\"some text\nwith more lines\n");
20           $fh = IO::Any->read('{"123":[1,2,3]}');
21           $fh = IO::Any->read('<root><element>abc</element></root>');
22           $fh = IO::Any->read(*DATA);
23           #$fh = IO::Any->read(IO::String->new("cba"));
24           #$fh = IO::Any->read($object_with_toString_method);
25
26           $fh = IO::Any->write('filename');
27           $fh = IO::Any->write('file://var/log/syslog');
28           #$fh = IO::Any->write('-');
29           $fh = IO::Any->write(['folder', 'filename']);
30           #$fh = IO::Any->write('=');
31           my $string;
32           $fh = IO::Any->write(\$string);
33
34           my $content = IO::Any->slurp(['folder', 'filename']);
35           IO::Any->spew(['folder2', 'filename'], $content);
36
37           perl -MIO::Any -le 'print IO::Any->slurp("/etc/passwd")'
38           perl -MIO::Any -le 'IO::Any->spew("/tmp/timetick", time())'
39

DESCRIPTION

41       The aim is to provide read/write anything. The module tries to guess
42       $what the "anything" is based on some rules. See "new" method Pod for
43       examples and "new" and "_guess_what" code for the implementation.
44
45       There are two methods "slurp" and "spew" to read/write whole $what.
46

MOTIVATION

48       The purpose is to be able to write portable one-liners (both
49       commandline and inside program) to read/write/slurp/spew
50       files/strings/$what-ever.  As I'm sick of writing
51       "File::Spec->catfile('folder', 'filename')" or "use Path::Class; dir();
52       file();".
53
54       First time I've used IO::Any for JSON::Util where for the function to
55       encode and decode files I can just say put as an argumen anything that
56       IO::Any accepts. It's then up to the users of that module to pass an
57       array if it's a file, scalar ref if it is a string or relay on the
58       module to guess $what.
59
60       Any suggestions, questions and also demotivations are more than
61       welcome!
62

METHODS

64   new($what, $how, $options)
65       Open $what in $how mode.
66
67       $what can be:
68
69                       'filename'                => [ 'file' => 'filename' ],
70                       'folder/filename'         => [ 'file' => 'folder/filename' ],
71                       'file:///folder/filename' => [ 'file' => '/folder/filename' ],
72                       [ 'folder', 'filename' ]  => [ 'file' => File::Spec->catfile('folder', 'filename') ],
73                       'http://a/b/c'            => [ 'http' => 'http://a/b/c' ],
74                       'https://a/b/c'           => [ 'http' => 'https://a/b/c' ],
75                       '{"123":[1,2,3]}'         => [ 'string' => '{"123":[1,2,3]}' ],
76                       '[1,2,3]'                 => [ 'string' => '[1,2,3]' ],
77                       '<xml></xml>'             => [ 'string' => '<xml></xml>' ],
78                       "a\nb\nc\n"               => [ 'string' => "a\nb\nc\n" ],
79                       *DATA                     => [ 'file' => *{DATA}{IO} ],
80
81       Returns filehandle. IO::String for 'string', IO::File for 'file'.
82       'http' not implemented jet :)
83
84       Here are alvailable %$options options:
85
86           atomic    true/false if the file operations should be done using L<IO::AtomicFile> or L<IO::File>
87           LOCK_SH   lock file for shared access
88           LOCK_EX   lock file for exclusive
89           LOCK_NB   lock file non blocking (will throw an excpetion if file is
90                         already locked, instead of blocking the process)
91
92   _guess_what
93       Returns ($type, $what). $type can be:
94
95           file
96           string
97           http
98           iostring
99           iofile
100
101       $what is normalized path that can be used for IO::*.
102
103   read($what)
104       Same as "IO::Any->new($what, '<');" or "IO::Any->new($what);".
105
106   write($what)
107       Same as "IO::Any->new($what, '>');"
108
109   slurp($what)
110       Returns content of $what.
111
112       If AnyEvent is loaded then uses event loop to read the content.
113
114   spew($what, $data, $opt)
115       Writes $data to $what.
116
117       If AnyEvent is loaded then uses event loop to write the content.
118

SEE ALSO

120       IO::All, File::Spec, Path::Class
121

AUTHOR

123       Jozef Kutej, "<jkutej at cpan.org>"
124

BUGS

126       Please report any bugs or feature requests to "bug-io-any at
127       rt.cpan.org", or through the web interface at
128       http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Any
129       <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-Any>.  I will be
130       notified, and then you'll automatically be notified of progress on your
131       bug as I make changes.
132

SUPPORT

134       You can find documentation for this module with the perldoc command.
135
136           perldoc IO::Any
137
138       You can also look for information at:
139
140       ·   GitHub: issues
141
142           http://github.com/jozef/IO-Any/issues <http://github.com/jozef/IO-
143           Any/issues>
144
145       ·   RT: CPAN's request tracker
146
147           http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-Any
148           <http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-Any>
149
150       ·   AnnoCPAN: Annotated CPAN documentation
151
152           http://annocpan.org/dist/IO-Any <http://annocpan.org/dist/IO-Any>
153
154       ·   CPAN Ratings
155
156           http://cpanratings.perl.org/d/IO-Any
157           <http://cpanratings.perl.org/d/IO-Any>
158
159       ·   Search CPAN
160
161           http://search.cpan.org/dist/IO-Any <http://search.cpan.org/dist/IO-
162           Any>
163

ACKNOWLEDGEMENTS

166       Copyright 2009 Jozef Kutej, all rights reserved.
167
168       This program is free software; you can redistribute it and/or modify it
169       under the same terms as Perl itself.
170
171
172
173perl v5.12.3                      2011-03-08                        IO::Any(3)
Impressum