私はPerlでプログラミングしていて、次のHTMLフォームからデータを取得する必要があります。
<FORM action="./cgi-bin/Perl.pl" method="GET">
<br>
Full name: <br><input type="text" name="full_name" maxlength="20"><br>
Username: <br><input type="text" name="user_name" maxlength="8"><br>
Password: <br><input type="password" name="password" maxlength="15"><br>
Confirm password: <br><input type="password" name="new_password" maxlength="15"><br>
<input type="submit" value ="Submit"><br><br>
</FORM>
編集:CGI.pmを使用できない場合、以下は機能しますか?
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}
else {
$buffer = $ENV{'QUERY_STRING'};
}
# Split information into name/value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
しかし、これらの値を使用しようとするたびにエラーが発生します。
Use of unitilialized value
CGIを使用してフォームデータを適切に処理するにはどうすればよいですか?
編集:私のエラーが他の場所にある可能性があります。これは私のコードです。それは私がgrepを使用している方法ですか? GETメソッドを使用しないでください。
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;
print "Content-type: text/html\n\n";
#getting these from HTML form
my $full_name = param('full_name');
my $user_name= param('user_name');
my $password = param('password');
my $new_password = param('new_password');
#checking that inputs are alphanumeric or an underscore
my $mismatch = grep /^[a-zA-Z0-9_]*$/i, $full_name, $user_name, $password, $new_password;
if($mismatch) {
#error message if invalid input
print qq(<html>\n);
print qq(<head>\n);
print qq(<title> Error: alphanumeric inputs only. </title>\n);
print qq{<meta http-equiv="refresh" content="5;URL="http://www.cs.mcgill.ca/~amosqu/registration.html">\n};
print qq(</head>\n);
print qq(<body>\n);
print qq(<b><center> Inputs with alphanumeric characters only please. </b></center>\n\n);
print qq(</body>\n);
print qq(</html>\n);
}
your previous questionに対する私の回答で提案した正規表現を変更しました。
grep /[^A-Z0-9]/i, $full_name, $user_name, $password, $new_password
これを変更して、
$mismatch
が有効なパラメーターの数に設定され、引数の無効なセットの条件が不自然な
$mismatch < 4
になるようにしました。
要件が英数字から英数字とアンダースコアに変更された場合は、
grep
の意味を次のようにして復元できます。
my $mismatch = grep /\W/, $full_name, $user_name, $password, $new_password
これは、値のいずれかに「非単語」文字が含まれている場合に
$mismatch
を正の真の値に設定します。
しかし、あなたが見ている問題
Use of uninitialized value $_ in pattern match (m//)
これは、パラメーター
$full_name
、
$user_name
、
$password
、または
$new_password
の少なくとも1つが未定義であるためです。あなたは、どれがなぜそれが起こっているのかを知る必要があります。 4つのクエリパラメータ
full_name
、
user_name
、
password
、および
new_password
がすべて、返されるクエリ文字列に含まれていることを確認しますか?
query_string
メソッドが何を返すか見てみましょう。