reading a select form from perl


V

Vit

Hi All,

I was just trying to read a webform to send an email.. (using
sendemail)

in the webform I have a selection form like that:

<select name="interest" style="height: 109px; width: 484px"
multiple="multiple">
<option selected="selected">&lt;--- Select One or More ---&gt;</
option>
<option>Option 1</option>
<option>Option 2</option>
</select>

I have notice that my perl code it breaks on this select form...

the perl code is the following:

#!/usr/bin/perl

use CGI;

# Create the CGI object
my $query = new CGI;

# Output the HTTP header
print $query->header ( );

# Capture the form results
my $company = $query->param("company");
my $enquiry = $query->param("enquiry");
my $interest = $query->param("interest");

# Filter the form results
$email_address = filter_header_field ( $email_address );
$enquiry = filter_field ( $enquiry );
#$interest = filter_field ( $interest );

# Email the form results
open ( MAIL, "| /usr/sbin/sendmail -t" );
print MAIL "From: $email_address\n";
print MAIL "To: test\@domain.com\n";
print MAIL "Subject: Web Request\n\n";
print MAIL "Comment: $enquiry\n";
#print MAIL "Interest: $interest\n"
print MAIL "\n.\n";
close ( MAIL );

# Thank the user
print <<END_HTML;
<html>
<head></head>
<body>Thanks for filling in our form!</body>
</html>
END_HTML

# Functions for filtering user input

sub filter_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
return $field;
}

sub filter_header_field
{
my $field = shift;
$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;
$field =~ s/[\0\n\r\|\!\/\<\>\^\$\%\*\&]+/ /g;
return $field;
}

how can I "read" the select form value??? how can I read if it is
multivalue???

thanks all

Vit
 
Ad

Advertisements

A

ace

Vit said:
how can I "read" the select form value??? how can I read if it is
multivalue???

use Data::Dumper;
my $multival = $query->param("multival");
print Dumper $multival;
 
T

Tad J McClellan

#!/usr/bin/perl


#!/usr/bin/perl
use warnings;
use strict;

my $query = new CGI;


my $query = CGI->new;

but that is a horrid choice of variable name IMO, so I'd do

my $cgi = CGI->new;

instead.

# Capture the form results


I usually grab all of them into a hash, and then use them from there:

my %param = $cgi->Vars;
my $enquiry = $query->param("enquiry");
$enquiry = filter_field ( $enquiry );


$param{enquiry} = filter_field( $param{enquiry} );

open ( MAIL, "| /usr/sbin/sendmail -t" );


You should always, yes *always*, check the return value from open().

print MAIL "From: $email_address\n";
print MAIL "To: test\@domain.com\n";
print MAIL "Subject: Web Request\n\n";
print MAIL "Comment: $enquiry\n";
#print MAIL "Interest: $interest\n"
print MAIL "\n.\n";


print MAIL <<ENDMAIL;
From: $email_address
To: test\@domain.com
Subject: Web Request

Comment: $enquiry

..
ENDMAIL

$field =~ s/From://gi;
$field =~ s/To://gi;
$field =~ s/BCC://gi;
$field =~ s/CC://gi;
$field =~ s/Subject://gi;
$field =~ s/Content-Type://gi;


$field =~ s/(From|To|BCC|CC|Subject|Content-Type)://gi;
 
T

Tad J McClellan

Vit said:
how can I read if it is
multivalue???


What happened when you searched for "multivalue" in the documentation
for the module that you are using?
 
V

Vit

use Data::Dumper;
my $multival = $query->param("multival");
print Dumper $multival;

thanks.. it seems to work... but unfortunatelly it's going to give me
just the first selected....

use Data::Dumper;
my $interest = $query->param("interest")
print MAIL Dumper $interest;

I get the following:
$VAR1 = '1';


how can I list all the option and show if they are selected or not...

thanks

Vit
 
P

Peter J. Holzer

I would split these two lines into three:

print MAIL "Subject: Web Request\n";
print MAIL "\n";
print MAIL "Comment: $enquiry\n";

Otherwise the end of the header is too easy to miss.
print MAIL <<ENDMAIL;
From: $email_address
To: test\@domain.com
Subject: Web Request

Comment: $enquiry

.
ENDMAIL

I strongly disagree. Here documents are horrible to read. They are
somewhat bearable if they are at the top level, but consider this:


sub process_form {
some();
code();
to();
get();
parameters();
if (everything_ok()) {
do();
some();
more();
open ( MAIL, "| /usr/sbin/sendmail -t" ) or die "...";
print MAIL <<ENDMAIL;
From: $email_address
To: test\@domain.com
Subject: Web Request

Comment: $enquiry

..
ENDMAIL
some();
more();
}
clean();
up();
}


I vastly prefer repeating 'print MAIL' on every line to to breaking the
visual structure of the program and never use here documents.

In this case I'd probably use a single print, though:

open ( MAIL, "| /usr/sbin/sendmail -t" ) or die "...";
print MAIL
"From: $email_address\n",
"To: test\@domain.com\n",
"Subject: Web Request\n",
"\n",
"Comment: $enquiry\n",
"\n",
".\n";


hp
 
Ad

Advertisements

T

Tad J McClellan

Peter J. Holzer said:
I strongly disagree. Here documents are horrible to read. They are
somewhat bearable if they are at the top level,


or if you apply the techniques given in the FAQ answer below so
that they are indented the same as the containing code:

perldoc -q here.doc

Why don't my <<HERE documents work?
 
U

Uri Guttman

TJM> or if you apply the techniques given in the FAQ answer below so
TJM> that they are indented the same as the containing code:

TJM> perldoc -q here.doc

TJM> Why don't my <<HERE documents work?

i love here docs and use them where many people wouldn't. i don't have
any issues with the indent of the closing token so that never bothers
me. the win for here docs is seeing your text as it will be printed
(other than interpolation) without noisy \n and quote chars and all. i
can see long lines and wrap them in the code. the syntax means you can
have the item comma right after the opening token vs way after the
closing quote. you can use here docs as args in a call and have much
cleaner code than a long quoted string followed by either more args or
the close of the call. the only issue i hear is the closing token but
that is nothing compared to all the wins i mentioned. and as tad and the
FAQ point out, you can even hack the indent at run time (i never do
that).

uri
 
T

Tad J McClellan

Uri Guttman said:
TJM> or if you apply the techniques given in the FAQ answer below so
TJM> that they are indented the same as the containing code:

TJM> perldoc -q here.doc

TJM> Why don't my <<HERE documents work?

i love here docs and use them where many people wouldn't. i don't have
any issues with the indent of the closing token so that never bothers
me. the win for here docs is seeing your text as it will be printed
(other than interpolation) without noisy \n and quote chars and all.
the only issue i hear is the closing token


An issue I had meant to mention but forgot was:

Here-docs can easily morph into a "templating system", and applying
"system" to here-docs isn't something that I am able to do with
a straight face.
 
P

Peter J. Holzer

TJM> or if you apply the techniques given in the FAQ answer below so
TJM> that they are indented the same as the containing code:

TJM> perldoc -q here.doc

TJM> Why don't my <<HERE documents work?

I know that FAQ entry. The proposed solution is fragile (it breaks
when you change the indentation) and replacing a constant with a
function call doesn't doesn't strike me as particularly elegant.
i love here docs and use them where many people wouldn't. [...]
the only issue i hear is the closing token but
that is nothing compared to all the wins i mentioned.

I don't care much about the closing token, I care about all the text in
the here document itself. I find a long block of text which isn't indented
along the rest of the code very distracting.

I like the way this is solved in SPL. Quoting from the manual:

| Another way of quoting strings in SPL are so-called Here-Documents. They
| work a little bit differently in SPL compared to other languages such as Perl.
|
| An example:
|
| debug <<EOT
| This is a debug message.
| It is quoted using the Here-Documents mechanism.
| Substitutions work here: 5 + 3 = ${ 5 + 3 }
| EOT;
|
| The Here-Document starts with ’<<Token’ and is terminated by the next
| occurrence of ’Token’. A white space character (i.e. also a newline character)
| after ’<<Token’ is ignored. Unlike in other languages, the terminating ’Token’
| doesn’t need to be at the beginning of a new line. Also note that the ’;’, which
| terminates the command, is located after the end token. The Here-Document
| starts immediately after ’<<Token’. The special substitutions (which are described
| in the next section) do work in this kind of Here-Document.
|
| If ’>>’ instead of ’<<’ is used, the Here-Document is literal and no substitutions
| are performed.
|
| The ’<<Token’ (or ’>>Token’) may be followed by any special character,
| which is then used as so-called indenting character. All characters from the
| beginning of a line in the Here-Document until the first occurrence of that
| indenting character are ignored:
|
| debug <<EOT:
| :This Here-Document is using indenting characters.
| :So there is no indenting in the output.
| EOT;

hp
 
U

Uri Guttman

TJM> An issue I had meant to mention but forgot was:

TJM> Here-docs can easily morph into a "templating system", and applying
TJM> "system" to here-docs isn't something that I am able to do with
TJM> a straight face.

and that happened to me. i started with a basic hash and s/// technique
using various strings including here docs. it evolved to something more
complex and when it hit 37 lines i had a neat little templater. the next
step was cpanning it into template::simple and it is now bloated to 150
lines!

uri
 
Ad

Advertisements

R

RedGrittyBrick

Peter said:
I would split these two lines into three:

print MAIL "Subject: Web Request\n";
print MAIL "\n";
print MAIL "Comment: $enquiry\n";

Otherwise the end of the header is too easy to miss.

I prefer one statement instead of three:

print MAIL "Subject: Web Request\n",
"\n",
"Comment: $enquiry\n";

But really I prefer a here-doc.

Just my ¤0.02 worth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top