On Thu, Mar 11, 2010 at 5:54 AM, Cosimo Streppone <[email protected]> wrote:
> I've been looking at how to do this for some time,
> but I seem to don't get it.
>
> If I have a $dbh handle, is it possible to know which
> hostname is that $dbh connected to?
>
> I know it's a bit stupid, because I'm supplying the
> hostname in the first place, but anyway...

Cosimo,

I use DBI::parse_dsn and then examine the "driver_dsn" string it
returns.  Since I pretty much only use MySQL, I know I can look for
the "host=foo" part of that:

my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn)
                = DBI->parse_dsn( $dsn );

if ( !$driver_dsn ) {
    die "No driver DSN from '$dsn'\n";
}

my $db_host;
if ( $driver_dsn =~ /host=(\w+)/ ) {
    $db_host = $1;
}
else {
    die "Can't figure out db host from '$driver_dsn'\n";
}

HTH,

ky

Reply via email to