[Pdx-pm] Apache, mod_perl, Catalyst, qx, and the ampersand

Michael G Schwern schwern at gmail.com
Wed Dec 20 16:39:14 PST 2006

Randall Hansen wrote:
> On Dec 20, 2006, at 3:35 PM, Roderick A. Anderson wrote:
>> 	qx{ script_that_does_the_get_and_other_magic & };
> that's one way.  here's another:
> http://www.stonehenge.com/merlyn/LinuxMag/col39.html
> i've used something like merlyn's fork technique several times and it  
> works well.  it's pretty easy to have some part of the app "notice"  
> when a process is finished and notify the user.

For those of use who never can remember which side of the if/else is the parent and which is the child and the idea of two conditions of a branch running simultaneously makes their brain melt and having all that forking housekeeping code scattered around, I like to do something like this....

sub worker (&) {
    my $worker = shift;

    if( my $pid = fork ) {  # parent
        ...do whatever housekeeping the parent needs to do...
    elsif( defined $pid ) { # child
        ...do whatever housekeeping the child needs to do...
    else {
        ...fork error...

Then just call it like so:

worker {
    ...child code...

This gives you a sane, generic forking routine which is easy to understand.  Into this you can pack the necessary voodoo to cleanly fork a child and cleanup the inevitable mess.

More information about the Pdx-pm-list mailing list