I have a daemon process that forks children. The code looks approximately like this (I'm omitting the parts that I think are irrelevant, but let me know if I shouldn't have done that.

use strict; start_daemon(); # this runs Proc::Daemon::Init() and sets up some SIG +stuff my $dbh = DBI->new(...); for(0..5){ my $pid = fork(); if ($pid) { next; } # Parent process: create the next child # Child: clone dbh, then do stuff my $childdbh = $dbh->clone(); $dbh->{InactiveDestroy}=1; undef $dbh; # Do stuff for(0..10){ subroutine($childdbh, @somevalues); } exit; # End of the child. } # Parent stuff goes here # Subroutines sub subroutine { my $childdbh = shift; $childdbh->do('this'); $childdbh->do('that'); $childdbh->do('the other'); another_subroutine( $childdbh, @othervalues ); } sub another_subroutine { my $childdbh = shift; $childdbh->do('even more stuff'); }

Ok, sorry about that long intro. My question is: what is the best practice for passing $childdbh to the subroutines? It seems awfully clunky to do it the way I've been doing it (where the first variable passed to any subroutine is the database handler). It works, but this is Perl -- there *must* be a more elegant way of doing it. :)


In reply to Best practices for passing $dbh to subroutines by xtpu

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.