0

I haven't been able to find an answer, nor am I sure if this is possible with perl but I believe that I should be able to do this.

I currently that a script where the user gives arguments like so:

perl X:\script.pl -ARG[0] "My Argument" -ARG[1] "Another Argument"

I would like to host this on my website and allow viewers to give their arguments through a HTML form, I have already read the Perl Docs for CGI however it doesn't seem to answer my question or I'm not understanding the document properly. To keep this question simple this is my script:

$name = "";
print "Hello $name";

How would I setup my HTML form to pass the argument to $name?

2
  • mediacollege.com/internet/perl/form-data.html This looks like a pretty simple explanation Commented Aug 21, 2012 at 18:05
  • @Forty-Two doing CGI yourself is very error-prone and soo 90-s. And reinventing the wheel. I posted a more robust solution as an answer. Commented Aug 21, 2012 at 18:55

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.