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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |