Conditionals can get confusing very quickly. Why not avoid them altogether? Here's the idea:

For each of your individual subroutines, you can define a private subroutine that actually does the work, plus a public subroutine that gets the database handle, calls the private subroutine, and then disconnects. For example:

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

If you get tired of writing the stub subroutines, you could autogenerate them with autoload. This would add some overhead. Namely, you could define an autoload subroutine like this one:

package your_package; sub AUTOLOAD { no strict qw(refs); our $AUTOLOAD; $AUTOLOAD =~ m{ (.*) :: (.*?) $ }x; my $dispatch_name = "$1::_do_$2"; if (defined &{$dispatch_name}) { *$AUTOLOAD = sub { my $dbh = &_get_dbh; my $rc = &$dispatch_name(@_); $dbh->disconnect(); return $rc; }; goto &$AUTOLOAD; } else { croak("Unable to autogenerate method for '$AUTOLOAD': no such +function '$dispatch_name'"); } }
That way, you could just define your _do_* subroutines, and the public versions would be autogenerated the first time they are called.

On the other hand, Masem's point (and others) are well taken. You can avoid doing all of the above by having your _get_dbh function work like so:

$your_package::DBH = undef; sub _get_dbh() { defined($your_package::DBH) and return $your_package::DBH; $your_package::DBH = DBI->connect... return $your_package::DBH; }

Update: Added autoload section.

Update: Added "on the other hand..."

stephen


In reply to Re: DBI: $dbh-disconnect; yes, now, now, but not now! by stephen
in thread 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.