That sounds really gruesome.
Perhaps, but I have been experimenting since I wrote the above, and I am
delighted. This makes so many things possible that I gave up on because I
couldn't fight my way through DBI. I rue the many hours I wasted at that.
@stuff=`mysql -e '$query'`;
foreach my $line (@stuff)
{
# process each returned line
}
is the moral equivalent of
$dbh=DBI::connect($dsn);
$sth=$dbh->prepare($query);
$sth->execute;
while ($row=$sth->fetch)
{
# process each row; $row is an arrayref
}
See what I mean? The direct version is simple, clean, easy to understand
and to edit. The objects are black boxes at the bottom of an opaque swamp:
there is no telling what they are doing or how.
except you avoid the shell, you avoid a connection per query, you can do
nice stuff with prepare and execute so that you can iterate a query over
a list (and also let execute automatically quote arguments appropriately
before submitting the query), and @$row is already parsed. If your
script is basically a one-off, backticks are fine, but anything larger
and backticks will become masochistic in a hurry.
Here's how I learned my lesson about objects:
I installed CGI::Kwiki. I thought it might be possible to customize it for a
project, although it was a horrible mess with javascript and all out of the
box. It said that it could be customized simply by writing replacement
modules and placing them in a local directory. Sounded good so far.
Well, the first thing I wanted to change was the DOCTYPE. I wanted to
change XHTML doctype to HTML. I grepped the distributed modules on XHTML,
locating all the doctype declarations in the distribution (all three
happened to be in template module). I rewrote the template module, put it
in the local lib and thought I was off to the races.
But no. Docs still came out as type XHTML. So I thought, well, maybe they
are rolling a doctype declaration out of parts. So I grepped on parts of
the XHTML doctype declaration, to see if I missed where the doctype really
was being generated. Nope. The doctype was not coming from anywhere in the
CGI::Kwiki distribution. Evidently it was coming from somewhere up the
dependencies. Where? After many hours I still do not know.
Somewhere, someone decided that I should want to use XHTML and that I was
not going to have a choice about it. And that is what OOP is all about:
preventing people from doing what they want to do.
When it become impossible to do anything in perl without objects, I'm
going back sed in Bourne scripts.