Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

IO::All is sliced bread

by DrWhy (Chaplain)
on Jun 09, 2006 at 21:44 UTC ( #554561=perlmeditation: print w/replies, xml ) Need Help??

I've been using IO::All for a few months now and I am now sure that it is the best thing since sliced bread. In fact it *is* sliced bread. It takes care of all the common niggliness that makes I/O such a pain in the anatomical part of your choice, wrapping it up in a neat, easy to use package. It does for I/O what Perl itself has done for data manipulation. It makes it painless so that for many, many applications you can let it worry about the vagaries of the systems requirements, error checking, etc. while you worry only about your algorithms. I think it should be distributed as a core module with Perl (It's not already, is it?).

P.S. No, Brian Ingerson didn't pay me to say that, and I'm not married to his sister.

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Replies are listed 'Best First'.
Re: IO::All is sliced bread
by zentara (Archbishop) on Jun 10, 2006 at 11:05 UTC
    and I'm not married to his sister

    But you may be trying to make browny points so you can get into her "code" at the next conference. ;-)


    I'm not really a human, but I play one on earth. flash japh
Re: IO::All is sliced bread
by DrWhy (Chaplain) on Jun 15, 2006 at 20:04 UTC
    arunbear suggested an example if how IO::All is helpful might be good. So here is one contrived example of making a copy of a file with the text reversed in a cross-patform manner (all code is untested):

    without IO:All:

    use File::Spec; my $file = File::Spec->catfile('my','dir','my_file.txt'); open (MYFILE, '<', $file) or die "Can't open $file: $!\n"; my @lines = <MYFILE>; close MYFILE; my $rfile = File::Spec->catfile('my','dir','reversed.txt'); open(RFILE, '>', $rfile) or die "Can't open $rfile: $!\n"; print RFILE reverse @lines; close RFILE;
    With IO::All:
    use IO::All; io->catdir('my','dir','my_file.txt')->backwards > io->catdir('my','dir','reversed.txt');
    To my mind the best thing about IO:All is that it takes care of all the error checking related to IO operations as well is the fussiness of opening and closing files in the correct mode for the desired operations.

    There are a number of other examples of how this module can be used to make your code simpler and more straightforward in the documentation that comes with IO::All.

    update: I misspoke in the opening paragaraph. This example reverses the lines in the file, the whole text is not reversed.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

      Since this is a conrtived example...here is my other contrived example. Not to say IO::All isn't cool, i think many of us are just bigger control freaks and can't stand to guess what the module will do and if it will do the right thing.

      use File::Spec; open (MYFILE, '<', File::Spec->catfile('my','dir','my_file.txt')) or d +ie "Can't open $file: $!\n"; open(RFILE, '>', File::Spec->catfile('my','dir','reversed.txt')) or di +e "Can't open $rfile: $!\n"; print RFILE reverse <MYFILE>;; use IO::All; my $file = io->catdir('my','dir','my_file.txt')->backwards; my $rfile = io->catdir('my','dir','reversed.txt'); $file > $rfile;

      Now if it was IO::DBD::All....just feed it a daatabase and a file of any format and ti does the right thing....well then i might not have much of a job so lets not go there.


      ___________
      Eric Hodges
      I thought this example:
      use IO::All; io->catdir('my','dir','my_file.txt')->backwards > io->catdir('my','dir','reversed.txt');
      was wrong until i read the IO::All documentation and found out that the ">" operator is overloaded by IO::All. You might write it more suggestively by saying:
      io->catdir('my','dir','my_file.txt')->backwards > io->catdir('my','dir +','reversed.txt');
        I dunno -- it got you to read about IO::All, didn't it ;)

        Actually, I wrote it that way on purpose because I didn't like the way the line was wrapped by PM when it was all on one line.

        --DrWhy

        "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://554561]
Approved by itub
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2021-10-18 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (75 votes). Check out past polls.

    Notices?