170 likes | 332 Views
Simple IO Handling with IO::All. Eitan Schuler – ExLibris. Introduction . Let ’ s read a file into one big string!. open my $file_handle, './Scotty' or die "Scotty can't be slurped:<br>$!"; local $/; # Set input to "slurp" mode. my $big_string = <$file_handle>; close $file_handle;. Agenda.
E N D
Simple IO Handling with IO::All Eitan Schuler – ExLibris Jerusalem.PM
Introduction • Let’s read a file into one big string! open my $file_handle, './Scotty' or die "Scotty can't be slurped:\n$!"; local $/; # Set input to "slurp" mode. my $big_string = <$file_handle>; close $file_handle; Jerusalem.PM
Agenda • The Basic idea • File assistance • Directory Assistance • Instead of File::Find • STDIN/STDOUT • Sockets • Fork Jerusalem.PM
The Basic Idea • The basic idea of IO::All is that it exports a function called io, which returns a new IO::All object. For example: use IO::All;my $io = io('file1');# Is the same thing as: my $io = IO::All->new('file1'); Jerusalem.PM
File Assistance • Reading a file backwards: • or my @reversed;my $io = io('file1.txt');$io->backwards;while (my $line = $io->getline) { push@reversed, $line;} my @reversed = io('file1.txt')->backwards->getlines; Jerusalem.PM
File Assistance • IO::All takes as many cues as possible from its context. • Here we are basically censoring a file, removing all the bad lines. • IO:All delays the open until the first IO operation and uses the operation to determine the context. my @lines = io('stuff')->slurp;my @good_lines = grep {not /bad/} @lines;io('good-stuff')->print(@good_lines); Jerusalem.PM
Directory Assistance • Let’s not use opendir, readdir, and closedir of perl. • IO::All opens a directory for reading and returns one entry at a time, much like readdir. BUT… my $dir = io('mydir');while (my $io = $dir->read) { print $io->name, "\n" if $io->is_file; } Jerusalem.PM
Directory Assistance • The difference is that instead of a file or subdirectory name, you get back another IO::All object. • We can immediately perform actions on the new objects. Jerusalem.PM
Instead of File::Find • There are all_files, all_dirs, all_links, all methods. my @wanted_file_names = map {$_->name} grep {$_->name =~ /\.\w{3}/ && $_->slurp =~ /ingy/ } io('my/directory')->all_files; Jerusalem.PM
A Poor Man's tar • ‘-r’– recursive • append use IO::All;for my $file (io('mydir')->all_files('-r')){ my $output = sprintf("--- %s (%s)\n", $file->name, -s $file->name) . $file->slurp; io('tar_like')->append($output); } Jerusalem.PM
STDIN and STDOUT • We read from STDIN and we write everything to STDOUT • ‘-’ can mean both, depending on the context io('-')->print(io('-')->slurp); Jerusalem.PM
Sockets • Since www.google.com:80 looks like a socket address, the IO::All object does the right thing. my $io = io('www.google.com:80');$io->print("GET / HTTP/1.1\n\n");print while ($_ = $io->getline) ne "\r\n"; Jerusalem.PM
Fork use IO::All;my $server = io(':12345')->accept('-fork'); $server->print($_) while <DATA>;$server->close;__DATA__One File, Two FileRed File, Blue File • This server sits and listens for connections on port 12345. When it gets a connection, it forks off a sub-process and sends two lines. Jerusalem.PM
Fork • This is the client use IO::All;my $io = io('localhost:12345');print while $_ = $io->getline; Jerusalem.PM
Other Functions • Locking of files ‘-lock’ • Easy to subclass by ‘–base’ • And many others… Jerusalem.PM
Summary • Try it!!!! • IO::All is a young module Jerusalem.PM
Where to Get More Information • http://search.cpan.org/~ingy/IO-All-0.16/lib/IO/All.pm • http://www.perl.com/pub/a/2004/03/12/ioall.html Jerusalem.PM