To do CGI with Perl, simply use the CGI module (comes pre-installed with Perl). It will parse form fields correctly and securely and handles both the get and post HTTP methods. This is far easier than coding or copy-and-pasting a solution. Just write a small CGI script that wraps your original script or rewrite your script to switch between a CGI and command-line mode.
HTML:
<form method="post" action="myScript.pl">
<input type="text" name="name" />
<input type="submit" value="Submit /">
</form>
Perl:
#!/usr/bin/perl
use strict; use warnings;
use CGI::Carp; # send errors to the browser, not to the logfile
use CGI;
my $cgi = CGI->new(); # create new CGI object
my $name = $cgi->param('name');
print $cgi->header('text/html');
print "Hello, $name";
You fetch your data fields with the param method. See the documentation for further variations. You can then invoke your script and return the output. You will probably use qx{…} or open to call your script and read the output. If you want to return the plain output you can try exec. Beware that it doesn't return (Content-type should be text/plain).
my $arg0 = $cgi->param('ARG[0]');
...;
my $output = qx{perl script.pl -ARG[0] "$arg0" -ARG[1] "$arg1"};
print $output;
or
my $arg0 = $cgi->param('ARG[0]');
...;
# prints directly back to the browser
exec 'perl', 'script.pl', '-ARG[0]', $arg0, '-ARG[1]', $arg1;
The first form allows you prettify your ourput, our decorate it with HTML. The second form is more efficient, and the arguments can be passed safely. However, the raw output is returned.
You can use system instead of exec if the original script shall print directly to the browser, but you have to regain the control flow, e.g. to close a <div> or whatever.