So, I don't know much about CGI. Your questions look pretty general to me, though. It seems you're asking about what incantations to use to put stuff in environment and redirect standard streams. Here is an example. I assume it's close enough to how CGI works. Note that there is no error handling whatsoever (I leave it as an exercise for the reader). Also note that it assumes Unix-like operating system.
# server.pl
use strict;
use warnings;
use POSIX ();
my $cgis = 0;
$SIG{CHLD} = sub {
while ( waitpid( -1, POSIX::WNOHANG ) > 0 ) {
$cgis -= 1;
}
};
while (1) {
print '(Ctrl-D to quit)> ';
my $request = <STDIN>;
last if not defined $request;
chomp $request;
print "Server: about to fork a CGI client\n";
$ENV{foo} = 'bar'; # use some real
$ENV{baz} = 'quux'; # CGI variables
pipe my ( $child_stdin, $input_to_child, );
pipe my ( $output_from_child, $child_stdout, );
my $pid = fork;
if ( $pid == 0 ) {
close $input_to_child;
close $output_from_child;
open STDIN, '<&', $child_stdin;
open STDOUT, '>&', $child_stdout;
exec {'perl'} 'perl', 'client.pl';
}
else {
close $child_stdin;
close $child_stdout;
print "Server: forked process $pid\n";
$cgis += 1;
print $input_to_child $request;
close $input_to_child;
print
"Server: reading child response\n",
<$output_from_child>;
}
}
print "Server exiting, remaining children: $cgis\n";
and client
# client.pl
use strict;
use warnings;
my $input = <STDIN>;
print <<END
This is a pseudo-CGI client reporting.
Process number $$
Got input "$input"
\$ENV{foo} is "$ENV{foo}"
\$ENV{baz} is "$ENV{baz}"
End of report
END