perlquestion
kudra
I've got a question which I hope someone can answer. The
problem has been solved, but I'm curious about why the
solution was neccessary.
<p>
Note: All examples are pseudocode.
<p>
I have been working on an application which runs in the fast CGI
environment. Much of the functionality has been put in modules.
There are a number of variables which need to be shared by the
modules in a single program run, but which must be distinct each
time the application is run. One of these variables is a DBI
handler, which I was passing to the constructors of two different
modules:
<code>
my $one = new One(dbh => $dbh, ...);
my $two = new Two(dbh => $dbh, ...);
</code>
Module One parses some files, begins a transaction, and calls routines
in module Two to insert some data. The commit is done in module One. The
handle is disconnected in the destruction of Two.
<p>
This did not work. It was almost as if One and Two had different connections,
because when I added a commit to Two, the data was committed. Yet I did not
get any warnings about the handle in Two being destroyed instead of
disconnected. In short, it didn't act like a new handler,
nor did it act like an old one.
<p>
I tried again to make sure that One and Two got the same handler.
I changed the code to:
<code>
my $one = new One(dbh => \$dbh, ...);
my $two = new Two(dbh => \$dbh, ...);
</code>
This worked.
<p>
My question is: why did I need to explicitly pass this by reference?