LPM: a problem with modularity using Net::LDAP

Tom Braun tombraun at aircontroltechniques.com
Mon Nov 6 10:02:04 CST 2000


An ojbect is just a blessed reference, right?  In that case, $dbh is
technically not a database handle, but a reference to a database handle.
Thus $dbh in your pressroomOpenDB sub does go out of scope, but the handle
it references is not destroyed since there is another reference to it,
namely the $dbh in your calling routine, which gets the value from the
return statement before $pressrom::dbh goes out of scope.  Recall that data
is not destroyed until all references to it go out of scope, not just the
first one.  Thus the handle stays around.   Aren't references fun :-)

Tom

>I'm doing something similar in my pressroom.  There are several programs
>that all use pressroom.pm, which includes:
>
>>pressroom.pm snippet:
>>use DBI;
>>
>>         ## returns a $db handle to the pressroom, or dies with a message.
>>sub pressroomOpenDB {
>>         my $DSN = "pressroom";
>>
>>         my $dbh = DBI->connect("DBI:ODBC:$DSN", '', '', {AutoCommit => 0,
>> PrintError=>1, RaiseError => 0});
>>
>>         unless ($dbh) {
>>             die "Error connecting to $DSN $!\n";
>>          }
>>         return $dbh;
>>}
>
>
>Then my calling programs have something like:
>
>scout.pl snippet:
>>use DBI;
>>use pressroom;          #page footers and other common subs.
>>
>>my $dbh = pressroomOpenDB();
>>
>>my $image_insert_sth = $dbh->prepare('insert into image (
>>         imageid, sourceext, releaseid, cutline, usage, imgapr, imgaprby
>>         ) values (?, ?, ?, ?, ?, ?, ?) ')
>>                 or die "Cannot prepare insert image.  $!";
>
>
>
>
>Only now that I read this email, I'm wondering how this works at all.  (It
>does work, btw.)  Seems like pressroomOpenDB's $dbh would go out of scope
>at the end of the sub.  Maybe it is, and I'm just luckily getting away with
>it?  Or does perl automagically realize that I'm still using that value and
>doing what I mean, not what I say?
>
>Hrmm...I thought I was going to be offering a solution, but I'm ending up
>more confused than I started.  Anybody care to set me straight?  Is my code
>a ticking time bomb?
>
>
>-dave  :-|




More information about the Lexington-pm mailing list