Rewriting perl applications

Michael Smith (mjs@cursci.co.uk)
Mon, 01 Jul 1996 16:38:44 +0100

Message-Id: <31D7F104.316C0670@cursci.co.uk>
Date: Mon, 01 Jul 1996 16:38:44 +0100
From: Michael Smith <mjs@cursci.co.uk>
To: fastcgi-developers@openmarket.com
Subject: Rewriting perl applications

I have been given the task of converting some perl code written by
others to use fastCGI (well actually I gave it to myself).  Most of this
I have done without any trouble whatsoever; however in some cases it
doesn't seem to be so easy.  I am having trouble with two scripts, one
of which is based on open2.pl (apparently a well-known perl routine):

---

sub main'open2 {
    local($kidpid);
    local($dad_rdr, $dad_wtr, @cmd) = @_;

    $dad_rdr ne '' 		|| die "open2: rdr should not be null";
    $dad_wtr ne '' 		|| die "open2: wtr should not be null";

    # force unqualified filehandles into callers' package
    local($package) = caller;
    $dad_rdr =~ s/^[^']+$/$package'$&/;
    $dad_wtr =~ s/^[^']+$/$package'$&/;

    local($kid_rdr) = ++$fh;
    local($kid_wtr) = ++$fh;

    pipe($dad_rdr, $kid_wtr) 	|| die "open2: pipe 1 failed: $!";
    pipe($kid_rdr, $dad_wtr) 	|| die "open2: pipe 2 failed: $!";

    if (($kidpid = fork) < 0) {
	die "open2: fork failed: $!";
    } elsif ($kidpid == 0) {
	close $dad_rdr; close $dad_wtr;
	open(STDIN,  "<&$kid_rdr");
	open(STDOUT, ">&$kid_wtr");
    	select((select($kid_rdr), $| = 1)[0]); # unbuffer pipe
    	select((select($kid_wtr), $| = 1)[0]); # unbuffer pipe
	warn "execing @cmd\n" if $debug;
	exec @cmd;
	die "open2: exec of @cmd failed";   
    } 
    close $kid_rdr; close $kid_wtr;
    select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
    $kidpid;
}
1; # so require is happy

---

I'm not sure what's going on here but I guess that the STDIN/STDOUT is
getting messed up by fastCGI; is there easy way to rewrite this?

The second file giving me strife is called by

	  bioserv(\%in,STDOUT);

and has lines in it like

sub bioserv
{
   local($in,$out) = @_;
   if ($out)
   {
      syswrite($out,$proceed,length($proceed));
   }

Would this be expected to work without alteration?

In general I am having trouble debugging as it is difficult to see what
fastCGI is doing to my scripts.  Any suggestions?

Thanks for any advice/help

Michael Smith