IO::All - IO::All of it to Graham and Damian!
use IO::All; # Let the madness begin...
# Some of the many ways to read a whole file into a scalar io('file.txt') > $contents; # Overloaded "arrow" $contents < io 'file.txt'; # Flipped but same operation $io = io 'file.txt'; # Create a new IO::All object $contents = $$io; # Overloaded scalar dereference $contents = $io->all; # A method to read everything $contents = $io->slurp; # Another method for that $contents = join '', $io->getlines; # Join the separate lines $contents = join '', map "$_\n", @$io; # Same. Overloaded array deref $io->tie; # Tie the object as a handle $contents = join '', <$io>; # And use it in builtins # and the list goes on ...
# Other file operations: @lines = io('file.txt')->slurp; # List context slurp $content > io('file.txt'); # Print to a file io('file.txt')->print($content, $more); # (ditto) $content >> io('file.txt'); # Append to a file io('file.txt')->append($content); # (ditto) $content << $io; # Append to a string io('copy.txt') < io('file.txt'); $ Copy a file io('file.txt') > io('copy.txt'); # Invokes File::Copy io('more.txt') >> io('all.txt'); # Add on to a file
# Print the path name of a file: print $io->name; # The direct method print "$io"; # Object stringifies to name print $io; # Quotes not needed here print $io->filename; # The file portion only
# Read all the files/directories in a directory: $io = io('my/directory/'); # Create new directory object @contents = $io->all; # Get all contents of dir @contents = @$io; # Directory as an array @contents = values %$io; # Directory as a hash push @contents, $subdir # One at a time while $subdir = $io->next;
# Print the name and file type for all the contents above: print "$_ is a " . $_->type . "\n" # Each element of @contents for @contents; # is an IO::All object!!
# Print first line of each file: print $_->getline # getline gets one line for io('dir')->all_files; # Files only
# Print names of all files/dirs three directories deep: print "$_\n" for $io->all(3); # Pass in the depth. Default=1
# Print names of all files/dirs recursively: print "$_\n" for $io->all(0); # Zero means all the way down print "$_\n" for $io->All; # Capitalized shortcut print "$_\n" for $io->deep->all; # Another way
# There are some special file names: print io('-'); # Print STDIN to STDOUT io('-') > io('-'); # Do it again io('-') < io('-'); # Same. Context sensitive. "Bad puppy" > io('='); # Message to STDERR $string_file = io('$'); # Create IO::String Object $temp_file = io('?'); # Create a temporary file
# Socket operations: $server = io('localhost:5555')->fork; # Create a daemon socket $connection = $server->accept; # Get a connection socket $input < $connection; # Get some data from it "Thank you!" > $connection; # Thank the caller $connection->close; # Hang up io(':6666')->accept->slurp > io->devnull; # Take a complaint and file it
# DBM database operations: $dbm = io 'my/database'; # Create a database object print $dbm->{grocery_list}; # Hash context makes it a DBM $dbm->{todo} = $new_list; # Write to database $dbm->dbm('GDBM_file'); # Demand specific DBM io('mydb')->mldbm->{env} = \%ENV; # MLDBM support
# Tie::File support: $io = io 'file.txt'; $io->[42] = 'Line Forty Three'; # Change a line print $io->[@$io / 2]; # Print middle line @$io = reverse @$io; # Reverse lines in a file
# Stat functions: printf "%s %s %s\n", # Print name, uid and size of $_->name, $_->uid, $_->size # contents of current directory for io('.')->all; print "$_\n" for sort # Use mtime method to sort all {$b->mtime <=> $a->mtime} # files under current directory io('.')->All_Files; # by recent modification time.
# File::Spec support: $contents < io->catfile(qw(dir file.txt)); # Portable IO operation
# Miscellaneous: @lines = io('file.txt')->chomp->slurp; # Chomp as you slurp @chunks = io('file.txt')->separator('xxx')->slurp; # Use alternnate record sep $binary = io('file.bin')->binary->all; # Read a binary file io('a-symlink')->readlink->slurp; # Readlink returns an object print io('foo')->absolute->pathname; # Print absolute path of foo
# IO::All External Plugin Methods io("myfile") > io->("ftp://store.org"); # Upload a file using ftp $html < io->http("www.google.com"); # Grab a web page io('mailto:worst@enemy.net')->print($spam); # Email a "friend"
# This is just the beginning, read on...
IO::All combines all of the best Perl IO modules into a single Spiffy object oriented interface to greatly simplify your everyday Perl IO idioms. It exports a single function called "io", which returns a new IO::All object. And that object can do it all!
The IO::All object is a proxy for IO::File, IO::Dir, IO::Socket, IO::String, Tie::File, File::Spec, File::Path and File::ReadBackwards; as well as all the DBM and MLDBM modules. You can use most of the methods found in these classes and in IO::Handle (which they inherit from). IO::All adds dozens of other helpful idiomatic methods including file stat and manipulation functions.
IO::All is pluggable, and modules like IO::All::LWP and IO::All::Mailto add even more functionality. Optionally, every IO::All object can be tied to itself. This means that you can use most perl IO builtins on it: readline, <>, getc, print, printf, syswrite, sysread, close.
The distinguishing magic of IO::All is that it will automatically open (and close) files, directories, sockets and other IO things for you. You never need to specify the mode ('<', '>>', etc), since it is determined by the usage context. That means you can replace this:
open STUFF, '<', './mystuff' or die "Can't open './mystuff' for input:\n$!"; local $/; my $stuff = <STUFF>; close STUFF;
with this:
my $stuff < io"./mystuff";
"abs2rel", "absolute", "accept", "All", "all", "All_Dirs", "all_dirs", "All_Files", "all_files", "All_Links", "all_links", "append", "appendf", "appendln", "assert", "atime", "autoclose", "autoflush", "backwards", "bcc", "binary", "binmode", "blksize", "blocks", "block_size", "buffer", "canonpath", "case_tolerant", "catdir", "catfile", "catpath", "cc", "chdir", "chomp", "clear", "close", "confess", "content", "ctime", "curdir", "dbm", "deep", "device", "device_id", "devnull", "dir", "domain", "empty", "eof", "errors", "file", "filename", "fileno", "filepath", "filter", "fork", "from", "ftp", "get", "getc", "getline", "getlines", "gid", "handle", "head", "http", "https", "inode", "io_handle", "is_absolute", "is_dir", "is_dbm", "is_executable", "is_file", "is_link", "is_mldbm", "is_open", "is_pipe", "is_readable", "is_socket", "is_stdio", "is_string", "is_temp", "is_writable", "join", "length", "link", "lock", "mailer", "mailto", "mkdir", "mkpath", "mldbm", "mode", "modes", "mtime", "name", "new", "next", "nlink", "open", "password", "path", "pathname", "perms", "pipe", "port", "print", "printf", "println", "put", "rdonly", "rdwr", "read", "readdir", "readlink", "recv", "rel2abs", "relative", "rename", "request", "response", "rmdir", "rmtree", "rootdir", "scalar", "seek", "send", "separator", "shutdown", "size", "slurp", "socket", "sort", "splitdir", "splitpath", "stat", "stdio", "stderr", "stdin", "stdout", "string", "string_ref", "subject", "sysread", "syswrite", "tail", "tell", "temp", "tie", "tmpdir", "to", "touch", "truncate", "type", "user", "uid", "unlink", "unlock", "updir", "uri", "utf8", "utime" and "write".
Each method is documented further below.
Even though relatively few operations are overloaded, there is actually a huge matrix of possibilities for magic. That's because the overloading is sensitive to the types, position and context of the arguments, and an IO::All object can be one of many types.
The most important overload to grok is stringification. IO::All objects stringify to their file or directory name. Here we print the contents of the current directory:
perl -MIO::All -le 'print for io(".")->all'
is the same as:
perl -MIO::All -le 'print $_->name for io(".")->all'
Stringification is important because it allows IO::All operations to return objects when they might otherwise return file names. Then the recipient can use the result either as an object or a string.
'>' and '<' move data between objects in the direction pointed to by the operator.
$content1 < io('file1'); $content1 > io('file2'); io('file2') > $content3; io('file3') < $content3; io('file3') > io('file4'); io('file5') < io('file4');
'>>' and '<<' do the same thing except the recipent string or file is appended to.
An IO::All file used as an array reference becomes tied using Tie::File:
$file = io"file"; # Print last line of file print $file->[-1]; # Insert new line in middle of file $file->[$#$file / 2] = 'New line';
An IO::All file used as a hash reference becomes tied to a DBM class:
io('mydbm')->{ingy} = 'YAML';
An IO::All directory used as an array reference, will expose each file or subdirectory as an element of the array.
print "$_\n" for @{io 'dir'};
IO::All directories used as hash references have file names as keys, and IO::All objects as values:
print io('dir')->{'foo.txt'}->slurp;
Files used as scalar references get slurped:
print ${io('dir')->{'foo.txt'}};
Not all combinations of operations and object types are supported. Some just haven't been added yet, and some just don't make sense. If you use an invalid combination, an error will be thrown.
use IO::All; my $io1 = io('myfile')->lock; $io1->println('line 1');
fork or do { my $io2 = io('myfile')->lock; print $io2->slurp; exit; };
sleep 1; $io1->println('line 2'); $io1->println('line 3'); $io1->unlock;
There are a lot of subtle things going on here. An exclusive lock is issued for $io1 on the first "println". That's because the file isn't actually opened until the first IO operation.
When the child process tries to read the file using $io2, there is a shared lock put on it. Since $io1 has the exclusive lock, the slurp blocks.
The parent process sleeps just to make sure the child process gets a chance. The parent needs to call "unlock" or "close" to release the lock. If all goes well the child will print 3 lines.
my $io = io'file1.txt'; $io->autoclose(1); while (my $line = $io->getline || $io->getline) { print $line; }
my @reversed; my $io = io('file1.txt'); $io->backwards; while (my $line = $io->getline) { push @reversed, $line; }
or more simply:
my @reversed = io('file1.txt')->backwards->getlines;
The "backwards" method returns the IO::All object so that you can chain the calls.
NOTE: This operation requires that you have the File::ReadBackwards module installed.
In this example, a server will return 3 lines of text, to every client that calls it. Here is the server code:
use IO::All;
my $socket = io(':12345')->fork->accept; $socket->print($_) while <DATA>; $socket->close;
__DATA__ On your mark, Get set, Go!
Here is the client code:
use IO::All;
my $io = io('localhost:12345'); print while $_ = $io->getline;
You can run the server once, and then run the client repeatedly (in another terminal window). It should print the 3 data lines each time.
Note that it is important to close the socket if the server is forking, or else the socket won't go out of scope and close.
perl -MIO::All -e 'io(":8080")->fork->accept->(sub { $_[0] < io(-x $1 ? "./$1 |" : $1) if /^GET \/(.*) / })'
There is are a lot of subtle things going on here. First we accept a socket and fork the server. Then we overload the new socket as a code ref. This code ref takes one argument, another code ref, which is used as a callback.
The callback is called once for every line read on the socket. The line is put into $_ and the socket itself is passed in to the callback.
Our callback is scanning the line in $_ for an HTTP GET request. If one is found it parses the file name into $1. Then we use $1 to create an new IO::All file object... with a twist. If the file is executable ("-x"), then we create a piped command as our IO::All object. This somewhat approximates CGI support.
Whatever the resulting object is, we direct the contents back at our socket which is in $_[0]. Pretty simple, eh?
io("names.db")->{ingy} > io'=';
Since their are several DBM formats available in Perl, IO::All picks the first one of these that is installed on your system:
DB_File GDBM_File NDBM_File ODBM_File SDBM_File
You can override which DBM you want for each IO::All object:
my @keys = keys %{io('mydbm')->dbm('SDBM_File')};
package NewModule; use IO::All -base;
You need to do it this way so that IO::All will export the "io" function. Here is a simple recipe for subclassing:
IO::Dumper inherits everything from IO::All and adds an extra method called "dump", which will dump a data structure to the file we specify in the "io" function. Since it needs Data::Dumper to do the dumping, we override the "open" method to "require Data::Dumper" and then pass control to the real "open".
First the code using the module:
use IO::Dumper;
io('./mydump')->dump($hash);
And next the IO::Dumper module itself:
package IO::Dumper; use IO::All -base; use Data::Dumper;
sub dump { my $self = shift; Dumper(@_) > $self; }
1;
IO::Dumper->import; io('./mydump')->dump($hash);
package IO::Dumper; use IO::All -base; use Data::Dumper;
sub dump { my $self = shift; Dumper(@_) > $self; }
io(file-descriptor); IO::All->new(file-descriptor); $io->new(file-descriptor);
All three forms take a single argument, a file descriptor. A file descriptor can be any of the following:
- A file name - A file handle - A directory name - A directory handle - A typeglob reference - A piped shell command. eq '| ls -al' - A socket domain/port. eg 'perl.com:5678' - '-' means STDIN or STDOUT (depending on usage) - '=' means STDERR - '$' means an IO::String object - '?' means a temporary file - A URI including: http, https, ftp and mailto - An IO::All object
If you provide an IO::All object, you will simply get that same object returned from the constructor.
If no file descriptor is provided, an object will still be created, but it must be defined by one of the following methods before it can be used for I/O:
io->file(file-name);
Using the "file" method sets the type of the object to file and sets the pathname of the file if provided.
It might be important to use this method if you had a file whose name was '-', or if the name might otherwise be confused with a directory or a socket. In this case, either of these statements would work the same:
my $file = io('-')->file; my $file = io->file('-');
io->file(dir-name);
Make the object be of type directory.
io->file(domain:port);
Make the object be of type socket.
io->file(link-name);
Make the object be of type link.
io->file(link-name);
Make the object be of type pipe. The following two statements are equivalent:
my $io = io('ls -l |'); my $io = io('ls -l')->pipe; my $io = io->pipe('ls -l');
io('mydbm')->dbm('NDBM_File', 'SDBM_File')->{author} = 'ingy';
If no module names are provided, the first available of the following is used:
DB_File GDBM_File NDBM_File ODBM_File SDBM_File
This method takes the names of zero or more DBM modules and an optional serialization module. The first DBM module that is available is used to process the MLDBM file. The serialization module can be Data::Dumper, Storable or FreezeThaw.
io('mymldbm')->mldbm('GDBM_File', 'Storable')->{author} = {nickname => 'ingy'};
my $io = io('$'); my $io = io->string;
my $io = io('-'); my $io = io->stdin;
io->handle(io-handle);
Forces the object to be created from an pre-existing IO handle. You can chain calls together to indicate the type of handle:
my $file_object = io->file->handle($file_handle); my $dir_object = io->dir->handle($dir_handle);
If you need to use the same options to create a lot of objects, and don't want to duplicate the code, just create a dummy object with the options you want, and use that object to spawn other objects.
my $lt = io->lock->tie; ... my $io1 = $lt->new('file1'); my $io2 = $lt->new('file2');
Since the new method copies attributes from the calling object, both $io1 and $io2 will be locked and tied.
Each option can take a single argument of 0 or 1. If no argument is given, the value 1 is assumed. Passing 0 turns the option off.
All of these options return the object reference that was used to invoke them. This is so that the option methods can be chained together. For example:
my $io = io('path/file')->tie->assert->chomp->lock;
This option is on by default, so if you don't want this behaviour, say so like this:
$io->autoclose(0);
The object will then be closed when $io goes out of scope, or you manually call "$io->close".
Requires the File::ReadBackwards CPAN module.
my $io = io('foo')->tie; @lines = <$io>;
If these methods are passed no argument, they will return their current value. If arguments are passed they will be used to set the current value, and the object reference will be returned for potential method chaining.
This is the buffer that "read" and "write" will use by default.
You can easily have IO::All objects use the same buffer:
my $input = io('abc'); my $output = io('xyz'); my $buffer; $output->buffer($input->buffer($buffer)); $output->write while $input->read;
my @odd = io->curdir->filter(sub {$_->size % 2})->All_Files;
@odd will contain all the files under the current directory whose size is an odd number of bytes.
$io->mode('>>')->open; $io->mode(O_RDONLY);
Returns a reference to the internal string that is acting like a file.
If the "fork" method was called on the object, the process will automatically be forked for every connection.
compare(io('file1')->all, io('file2')->all);
'.' and '..' are excluded.
Takes an optional argument telling how many directories deep to search. The default is 1. Zero (0) means search as deep as possible.
The filter method can be used to limit the results.
The items returned are sorted by name unless "->sort(0)" is used.
io('my/path/file.txt')->filename;
would return "file.txt".
io('my/path/file.txt')->filename;
would return "my/path".
NOTE: Normally you won't need to call open (or mode/perms), since this happens automatically for most operations.
For a file, this will proxy IO::File::read. This means you must pass it a buffer, a length to read, and optionally a buffer offset for where to put the data that is read. The function returns the length actually read (which is zero at EOF).
If you don't pass any arguments for a file, IO::All will use its own internal buffer, a default length, and the offset will always point at the end of the buffer. The buffer can be accessed with the "buffer" method. The length can be set with the "block_size" method. The default length is 1024 bytes. The "clear" method can be called to clear the buffer.
For a directory, this will proxy IO::Dir::read.
Note that "readdir" does not return the special "." and ".." entries.
my $new = $io->rename('new-name');
Calls Perl's rename function and returns an IO::All object for the renamed file. Returns false if the rename failed.
file dir link socket string pipe
Returns undef if type is not determinable.
NOTE: You can unlink a file after it is open, and continue using it until it is closed.
NOTE: When used with the automatic internal buffer, "write" will clear the buffer after writing it.
IO::All->case_tolerant;
or more simply:
io->case_tolerant;
my $contents = io->catfile(qw(dir subdir file))->slurp;
This is a very portable way to read "dir/subdir/file".
my $io = io('mydbm.db'); $io->dbm('DB_File'); $io->{foo} = 'bar';
In the first statement, $io has a reference value of 'IO::All::File', if "mydbm.db" exists. In the second statement, the object is reblessed into class 'IO::All::DBM'.
To keep input objects from closing at EOF, do this:
$io->autoclose(0);
This would set $xxx to the IO::All object:
my $xxx = $contents > io('file.txt');
While this would set $xxx to the content string:
my $xxx = $contents < io('file.txt');
IO is tricky stuff. There is definitely more work to be done. On the other hand, this module relies heavily on very stable existing IO modules; so it may work fairly well.
I am sure you will find many unexpected ``features''. Please send all problems, ideas and suggestions to ingy@cpan.org.
If you call a method that does not make sense for a particular object, the result probably won't make sense. Little attempt is made to check for improper usage.
Also check out the Spiffy module if you are interested in extending this module.
Special thanks to Ian Langworth for continued testing and patching.
Thank you Simon Cozens for tipping me off to the overloading possibilities.
Finally, thanks to Autrijus Tang, for always having one more good idea.
(It seems IO::All of it to a lot of people!)
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |