Girish Tiwari Girish Tiwari - 1 year ago 62
Perl Question

PERL/CGI- gets more than text from input textarea

I'm not at all familiar with perl, but have some understanding of html. I'm currently trying to configure code from an online program that processes text inputted from the user to calculate and output a few important numbers in order to do the same for a large number of files containing text in a local directory. The problem lies in my lack of understanding for how or why the code from the site is splitting the inputted text by looking for & and =, as the inputted text never contains these characters, and neither my files. Here's some of the code from the online program:

&error('Error','Use Standard Input by METHOD=POST');

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

if ($buffer eq '') {
&error('Error','Can not execute directly','Check the usage');

$ref = $ENV{'HTTP_REFERER'};

@pairs = split(/&/,$buffer);

foreach $pair (@pairs) {
($name,$value) = split(/=/,$pair);
if ($name eq "ATOMS") { $atoms = $value; }

It then uses these "pairs" to appropriately calculate the required numbers. The input from the user is simply a textarea named "ATOMS", and the form action is the cgi script:

<form method=POST action="/path/to/the/cgi/file.cgi">
<textarea name="ATOMS" rows=20 cols=80></textarea>

I've left out the less important details of both the html and perl codes. So far all I've been able to do is get all the content from all files in a given directory in a text format, but when I input this into the script that uses the text from textarea to calculate the values (in place of the variable $buffer), it doesn't work, which I suspect is due to the split codes, which cannot find the & and = symbols. How does the code get these symbols from the online script, and how can I implement that to use for my local files? Let me know if any additional information is needed, and thanks in advance!

Answer Source

The encoding scheme forms use (by default) to POST data over HTTP consists of key=value pairs (hence the =) which are separated by & characters.

The latter doesn't much matter for your form since it has only one control in it.

This is described pretty succinctly in the HTML 4 specification and in more detail in the HTML 5 specification.

If you aren't dealing with data from a form, you should remove all the form decoding code.