Asker says:
I also want the frame to exist only after the result is generated/printed.
This makes it tricky, despite that CGI.pm does have support for frames. You didn't cite why you want to use a frameset, so you'll have to decide whether using a frameset approach is really worth the trouble.
You need to use conditionals and supplemental info to control what is output when: when the query form is printed, when the frameset is printed, and when the individual frames are printed. The trick is to output the frameset at the desired time with the frames pointed at back at the script with pathinfo to indicate which HTML/frame to output.
First, see about CGI.pm support for frames here:
Using frames effectively can be tricky. To create a proper frameset in which the query and response are displayed side-by-side requires you to divide the script into three functional sections. The first section should create the declaration and exit. The second section is responsible for creating the query form and directing it into the one frame. The third section is responsible for creating the response and directing it into a different frame.
I tried to modify http://stein.cshl.org/WWW/CGI/examples/frameset.txt to try to do what you want to do, but I haven't/can't test it (no CGI.pm server readily available). I seriously doubt it will work without some debugging. But hopefully, this gives you the basic idea to run with it. First study http://stein.cshl.org/WWW/CGI/examples/frameset.txt and then see my changes below:
#!/usr/local/bin/perl
### UNTESTED CODE ###
use CGI;
$query = new CGI;
print $query->header;
$TITLE="Frameset Example";
# We use the path information to distinguish between calls
# to the script to:
# (1) create the frameset
# (2) create the query form
# (3) create the query response
$path_info = $query->path_info;
# If no path information is provided, then we create
# print query form ###new####
# a side-by-side frame set ###old###
if (!$path_info) {
#&print_frameset; ###old###
&print_html_header; ###new###
&print_query ###new###
&print_end; ###new###
exit 0;
}
# If response path ###new###
if ($path_info=~/response/) { ###new###
&print_frameset; ###new###
exit 0; ###new###
} ###new###
# If we get here, then we either create the query form
# or we create the response.
&print_html_header;
#&print_query if $path_info=~/query/; ###old###
#&print_response if $path_info=~/response/; ###old###
&print_query if $path_info=~/frame-query/; ###new###
&print_response if $path_info=~/frame-response/; ###new###
&print_end;
# Create the frameset
sub print_frameset {
$script_name = $query->script_name;
print <<EOF;
<html><head><title>$TITLE</title></head>
<frameset cols="50,50">
<!--frame src="$script_name/query" name="query"--> <!--###old###-->
<!--frame src="$script_name/response" name="response"--> <!--###old###-->
<frame src="$script_name/query" name="frame-query"> <!--###new###-->
<frame src="$script_name/response" name="frame-response"> <!--###new###-->
</frameset>
EOF
;
exit 0;
}
sub print_html_header {
print $query->start_html($TITLE);
}
sub print_end {
print qq{<P><hr><A HREF="../index.html" TARGET="_top">More Examples</A>};
print $query->end_html;
}
sub print_query {
$script_name = $query->script_name;
print "<H1>Frameset Query</H1>\n";
#print $query->startform(-action=>"$script_name/response",-TARGET=>"response"); ###old###
print $query->startform(-action=>"$script_name/response"); ###new###
print "What's your name? ",$query->textfield('name');
print "<P>What's the combination?<P>",
$query->checkbox_group(-name=>'words',
-values=>['eenie','meenie','minie','moe']);
print "<P>What's your favorite color? ",
$query->popup_menu(-name=>'color',
-values=>['red','green','blue','chartreuse']),
"<P>";
print $query->submit;
print $query->endform;
}
sub print_response {
print "<H1>Frameset Result</H1>\n";
unless ($query->param) {
print "<b>No query submitted yet.</b>";
return;
}
print "Your name is <EM>",$query->param(name),"</EM>\n";
print "<P>The keywords are: <EM>",join(", ",$query->param(words)),"</EM>\n";
print "<P>Your favorite color is <EM>",$query->param(color),"</EM>\n";
}