1 / 17

Simple IO Handling with IO::All

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 &quot;Scotty can't be slurped:<br>$!&quot;; local $/; # Set input to &quot;slurp&quot; mode. my $big_string = &lt;$file_handle&gt;; close $file_handle;. Agenda.

edna
Download Presentation

Simple IO Handling with IO::All

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple IO Handling with IO::All Eitan Schuler – ExLibris Jerusalem.PM

  2. 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

  3. Agenda • The Basic idea • File assistance • Directory Assistance • Instead of File::Find • STDIN/STDOUT • Sockets • Fork Jerusalem.PM

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Fork • This is the client use IO::All;my $io = io('localhost:12345');print while $_ = $io->getline; Jerusalem.PM

  15. Other Functions • Locking of files ‘-lock’ • Easy to subclass by ‘–base’ • And many others… Jerusalem.PM

  16. Summary • Try it!!!! • IO::All is a young module Jerusalem.PM

  17. 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

More Related