xtpu has asked for the wisdom of the Perl Monks concerning the following question:
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. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Best practices for passing $dbh to subroutines
by choroba (Cardinal) on Jan 26, 2014 at 09:33 UTC | |
by CountZero (Bishop) on Jan 26, 2014 at 16:21 UTC | |
by choroba (Cardinal) on Jan 27, 2014 at 09:20 UTC | |
|
Re: Best practices for passing $dbh to subroutines (subroutine arguments)
by Anonymous Monk on Jan 26, 2014 at 09:02 UTC | |
|
Re: Best practices for passing $dbh to subroutines
by McA (Priest) on Jan 26, 2014 at 17:22 UTC |