> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 02, 2001 9:44 AM
> To: [EMAIL PROTECTED]
> Subject: Valid DB Handle Assertion Test
>
>
>
> Can I test for the existence of a method without actually calling it?
I don't know. Maybe, but some methods are implemented via AUTOLOAD, so may
not "exist" in the sense you mean.
>
> Basically what I'm trying to do is to verify that $dbh is
> still a valid
> handle.
> I want to use this as an assertion test at the start of a function.
>
> # verify that the $dbh is valid
> if ( !defined($dbh->disconnect() ) )
> {
> print "dbh is invalid\n" ;
> exit 1
> }
Well, this won't work, becuase it's testing the result returned from calling
$dbh->disconnect.
You have 3 possiblities with $dbh:
1. $dbh is a reference to a connected database handle.
2. $dbh is a reference to an unconnected database handle.
3. $dbh is not a reference to a database handle at all.
You can use perl's ref() function to distinguish the first two cases from
the last. see
perldoc -f ref
Once you know you have a database handle, you can use $dbh->{Active} to
tell whether it is connected (per DBI docs, although this may vary from
driver to driver). Actually, calling disconnect on an unconnected handle
seems to be harmless and treated as a no-op.
Another approach is to wrap the disconnect in eval, which lets you capture
the error rather than having it stop your program:
eval { $dbh->disconnect(); };
If $dbh is not a database handle, the relevant error message will be in $@.
perldoc -f eval
>
> # $dbh is valid so we can safely disconnect
> $dbh->disconnect()
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]