I owe it all to you, Perl’s IO::All

What is malleable is always superior to that which is immovable.

(So says Lao Tzuhttp://quote.robertgenn.com/auth_search.php?a…, apparently)

One of Perl’shttp://www.perl.org/ http://en.wikipedia.org/wiki/Perl great strengths is its flexibilityhttp://en.wikipedia.org/wiki/There’s_more_tha…. Another is the number of well written, mature libraries availablehttp://search.cpan.org/. Another is its own long historyhttp://www.linuxjournal.com/article/3394 with a large user base. This makes it well tested, and means there are lots<a class=”reflink” href=”http://www.google.co.uk/search?hl=en&q=perl+code+examples” title=”Google search for “perl code examples” brought up 13,000,000 hits on the day I did it, “C++ code examples” brought up 2,450,000; “python code examples” 9,370,000; “ruby code examples” got 1,200,000; but “C# code examples” won the day with 23,100,000.”>http://www.google.co.uk/search?hl=en&q=perl+c…</a> of examples out there on the net.

Every strength can also be a weakness

Because it has such a long history and it has lots of examples, there are also a lot of very old examples out there, and, a particular weakness of material published on the internet is the lack of a published date. When you add this to the large number of libraries, and that there are so many ways to achieve a task<a class=”reflink” href=”http://www.shlomifish.org/lecture/Perl/Lightning/Too-Many-Ways/slides/” title=”Shlomi Fish’s slides for “There are too many ways to do it” lightning talk”>http://www.shlomifish.org/lecture/Perl/Lightn…</a>, it means that the way the article tells you to do it may be quite old and outmoded. It’s hard for you to tell which is the best way if you’re not very familiar with the language.

Since I dip in and out of the world of Perl, it never being the main focus of a job I’ve had but always being extremely useful to every job I’ve had (even before I knew any Perl - how I wish I’d known it from birth) every time I come back to it there are certain bits I’ve forgotten.

The syntax for, and how best to open and read/write from a file is something I always forget.


No bother, I’ll just look it up on the internet, like I did last week… Oh, should I use the core Perl method openhttp://perldoc.perl.org/functions/open.html as it says to here?http://www.perlfect.com/articles/perlfile.sht…

open FILE, "filename.txt" or die $!;
while () {
    print $_;

Maybe I should use sysopenhttp://perldoc.perl.org/functions/sysopen.htm… as it suggests here?http://www.tizag.com/perlT/perlfilehandle.php…


print "content-type: text/html \n\n"; #The header
$FilePath = "home/html/myhtml.html"
sysopen(HANDLE, $FilePath, O_RDWR);
printf HANDLE "Welcome to Tizag!";
close (HANDLE);

Hmmm, that perldoc page was a bit confusing. A bit more Google searching and I find the Filehandle packagehttp://perldoc.perl.org/FileHandle.html. It looks nice, and it uses file references instead of the upper case handles, which allows you to pass them around like any other scalar variable.

use FileHandle;
$fh = FileHandle->new;
if ($fh->open("< file")) {
print ;

But here’s a tutorialhttp://www.davidpashley.com/articles/perl-io-… on filehandling that uses IO::Handleshttp://perldoc.perl.org/IO/Handle.html

(from IO::Handle perldoc reference)

use IO::Handle;
$io = new IO::Handle;
if ($io->fdopen(fileno(STDIN),"r")) {
print $io->getline;

Oh, it says in the perldoc:

IO::Handle is the base class for all other IO handle classes. It is not intended that objects of IO::Handle would be created directly, but instead IO::Handle is inherited from by several other classes in the IO hierarchy.

Right. Slightly confusing. Ah, I see now, the article actually uses IO::Filehttp://perldoc.perl.org/IO/File.html to create filehandles. It must inherit from IO::Handle.

(from IO::File perldoc reference)

use IO::File;
$fh = new IO::File;
if ($fh->open("< file")) {
print ;

Well, that’s slightly nicer looking code than the IO::Handle example, but I preferred the FileHandle example, and it says in the Filehandle doc:

There are many other functions available since FileHandle is descended from IO::File, IO::Seekable, and IO::Handle. Please see those respective pages for documentation on more functions.

So Filehandle it is!

But wait, it says in the IO::Handle doc:

If you are reading this documentation, looking for a replacement for the FileHandle package, then I suggest you read the documentation for IO::File too.

Why would I be looking for a replacement for Filehandle? Should I be using IO::File after all????

Iain is very confused now

Oh dear

I don’t get any less confused either. There are so many different ways to do this (thanks Larry) and so many different articles, mostly un-dated. I wish I knew what was the latest way, the one that the pro’s will be using, something really nice and clean and easy.

Step forward IO:All

Luckily, my code required a slurp, and after reading this helpful slurping articlehttp://www.perl.com/pub/a/2003/11/21/slurp.ht…, I found another articlehttp://www.perl.com/pub/a/2004/03/12/ioall.ht… on IO::All by the author, Brian Ingerson.

Now, I don’t know if this is the latest and greatest Perl module for file handling, but from the look of it this is the way I’d like to do things. Check out some examples (more herehttp://search.cpan.org/~ingy/IO-All-0.39/lib/… )

my $big_string = io('./Scotty')->slurp;

Yep, that’s it. A slurp in an easy to read, easy to do way. Want to write to a file?

$content > io('file.txt'); # Print to a file
$content >> io('file.txt'); # Append to a file

Super easy! I’m a big fan already, and I haven’t even got to some of the more interesting stuff that Brian Ingerson writes about in his article. I’m especially looking forward to using it with File::Find, as he’s right, the interface for that can be madness, and I always forget that too.

One thing I’ve learnt this past week (or two) using a lot of Perl, is that once you’ve found one way to do things then it’s a good time to have a quick search of CPAN and see what other modules have been written to solve your problems. Be a little bit sideways in your thinking, for example, if you search for “file” or “filehandle” you won’t see IO::All pop up in the results list (at least soon enough for you to notice). Have a quick look at the modules and method names in the solution you already have. Here the key word is “IO”. It also seems that CPAN search is case sensitive (not the best idea, I think) so try a few different ways.

It also helps to look for modules with Any, Util, and All in their names.

Added on:
Last updated:
Info:System at time of writing - OSX 10.6 - Perl is version 5.10.1 YMMV