Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

Note: All examples are pseudocode.

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:

my $one = new One(dbh => $dbh, ...); my $two = new Two(dbh => $dbh, ...);
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.

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.

I tried again to make sure that One and Two got the same handler. I changed the code to:

my $one = new One(dbh => \$dbh, ...); my $two = new Two(dbh => \$dbh, ...);
This worked.

My question is: why did I need to explicitly pass this by reference?


In reply to Why was it neccessary to pass a DBI handler by reference? by kudra

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-06-22 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.