I am creating a Perl Module that has atleast 3 sub routines that could be called externally. One of these routines calls all the rest of the routines. So you could get a routine to run in two ways: by calling it directly or by calling the "all" routine.

Now here is my problem: Each routine connects to the database using DBI or the "all" routine connects and passes the database handle along. I am trying to determine when I should disconnect from the database when calling the routines in the 2 scenarios.

Here are some code snippets

sub make_all_reports { # connect to the database my $dbh = &_get_dbh; &make_summary_report($dbh); &make_report1($dbh); &make_report1($dbh); ... # close the database connection $dbh->disconnect; # return happy and healthy return (1); }

That was for reference, my problem is in the next routine.

sub make_report1 { # connect to the database my $dbh = shift; $dbh = &_get_dbh if (!defined($dbh)); ... my $caller = caller(1); # close the database connection if opened here $dbh->disconnect if (!defined $caller); # return happy and healthy return (1); }

OK, so I need the conditional to work better on the database disconnect. I need to only disconnect if the $dbh was not passed in as a argument from a caller routine. If someone calls this routine from a different package (even main:: ?) it could break.

Any help you could provide would be wonderful, thank you


In reply to DBI: $dbh-disconnect; yes, now, now, but not now! by DeaconBlues

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.