I have the following code in in a Perl script I’m writing:
#!/usr/bin/perl
use DBI;
use Getopt::Long;
use Time::HiRes;
use Error qw(:try);
....
my $starttime = Time::HiRes::time;
try {
my $dbh = DBI->connect("dbi:Sybase:$server", $username, $password);
$dbh->do("use $database");
my $query="exec OfflineDatabaseReport";
$row = $dbh->selectrow_hashref($query);
$dbh->disconnect();
} catch Error with {
print "boom";
exit 1;
};
my $endtime = Time::HiRes::time;
my $timetaken = $endtime - $starttime;
The script worked fine until I wrapped the data access portion in the try...catch block. Now I get the following exception thrown:
Can’t use string (“1316135985.90893”) as a HASH ref while “strict refs” in use at /usr/lib/perl5/site_perl/5.8.8/Error.pm line 217.
I did try setting:
no strict "refs";
But I still get the same error. Am I being naive in my usage of a try/catch block here?
This is how the parser sees the code:
Meaning, it passes the result of setting
$endtimetoTime::HiRes::timeas the 2nd argument of thewith BLOCKsub. Looking at the source ofError.pm, I see:Which means
with BLOCK,SCALARis a valid argument list. All it does is pass the arguments up tocatch, which interpretsmy $endtime = Time::HiRes::timeas its$clauses.catchitself returns$clauses, which turns the whole statement into:tryassumes$clausesis a hashref, as you can see by the call toSo perl tries to use the value of
Time::HiRes::timeas a hashref, which it most certainly cannot, since it’s actually a scalar with a value of “1316135985.90893”.So yeah, the semicolon at the end of the
catchblock.