Okay, my simplified module wouldn't loose the value of the package-level
lexical, but here's some output from my DBI library, which still looses
the value:

DBH Before Prepare: undef
DBH Before Connect: undef
DBH Connecting...
DBH After Connect: Apache::DBI::db=HASH(0x12d2214)
DBH After Prepare Connect: Apache::DBI::db=HASH(0x12d2214)

DBH Before selectcol_arrayref connect: undef
DBH Before Connect: Apache::DBI::db=HASH(0x12d2214)
DBH After Connect: Apache::DBI::db=HASH(0x12d2214)
DBH After selectcol_arrayref connect: undef


The code that triggers these events looks like this:

dbi_prepare($sql);
dbi_selectcol_arraryref($sql, @params);

The dbi_prepare function looks like this:

sub dbi_prepare {
    my $qry = shift;
    print STDERR "DBH Before Prepare: ", $dbh || 'undef', "\n";
    _connect();
    print STDERR "DBH After Prepare Connect: ", $dbh || 'undef', "\n";
    return $dbh->prepare($qry);
} # dbi_prepare()


_connect looks like this:

sub _connect {
    print STDERR "DBH Before Connect: ", $dbh || 'undef', "\n";
    unless ($dbh && $dbh->ping) {
        print STDERR "DBH Connecting...\n" if $DEBUG;
        $dbh = DBI->connect("DBI:$DBD:$DSN", $ID, $PASS, $ATTR) or die
$DBI::errstr;
    }
    print STDERR "DBH After Connect: ", $dbh || 'undef', "\n";
} # _connect()

So what happens is that dbi_prepare calls _connect() before it does
anything else. Then the $dbh can be expected to persist beyond the call
to _connect() because it's a package-level lexical. And indeed, it does
persist in dbi_prepare, where it is used to prepare the SQL statement.
dbi_selectcol_arraryref() looks like this:

sub dbi_selectcol_arrayref {
    my ($qry, @params) = @_;
    print STDERR "DBH Before connect: ", $dbh || 'undef', "\n";
    _connect();
    print STDERR "DBH After selectcol_arrayref connect: ", $dbh ||
'undef', "\n";
    $qry = dbi_prepare($qry) if ref $qry ne 'DBI::st';
    return $dbh->selectcol_arrayref($qry, undef, @params);
} # dbi_selectcol_arrayref()

Simple, eh? Yet the print statments (which I collected form the Apache
Error Log) clearly show that $dbh has no value when we enter
dbi_selectcol_arrayref(), but the first thing dbi_selectcol_arrayref()
does is call _connnect(), where it *does* still have a value! Returning
to dbi_selectcol_arrayref(), the value is missing again.

I'll keep working on trynig to create a simpler script that others can
try on their systems.

Thanks,

David

Reply via email to