简体   繁体   English

Perl哈希声明错误

[英]perl hash declaration error

I am new to perl and can't seem to find why this snippet is giving me a 500 error. 我是perl的新手,似乎无法找到为什么此片段给了我500错误。

#!/usr/bin/perl
use strict;
use warnings;
use CGI::Carp qw( fatalsToBrowser );

my ($distance, $weight, $total_gas, $mph, $buffer, $pair, @pairs, $value, $form, $name);
our %FORM = ();

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

@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

Everything I tried on the %FORM = (); 我在%FORM = ();上尝试过的所有内容%FORM = (); gives me variable declaration errors. 给我变量声明错误。

Are you certain that @pairs contains the values you expect (ie that they are name value pairs split with "="? More than likely $name isn't defined and you can't add an undefined key pair to a hash. Why are you using STDIN to read in values from the query string? Try: 您确定@pairs包含您期望的值(即,它们是用“ =”分隔的名称值对吗?很有可能未定义$ name,也无法将未定义的密钥对添加到哈希中。为什么您使用STDIN读取查询字符串中的值吗?

my $q = CGI->new;
my @keys = $q->param;
my %FORM;
foreach my $name (@keys)
{
    my $value = $q->param($name);
    $FORM{$name} = $value;
}

or 要么

my $q = CGI->new;
my %FORM = $q->Vars;

http://perldoc.perl.org/CGI.html http://perldoc.perl.org/CGI.html

I think you're missing HTTP header. 我认为您缺少HTTP标头。 Try adding to put following line before any print: 尝试在任何打印之前添加以下行:

print "Content-type: text/html\n\n";

Make sure you have enough permissions for script to run. 确保您具有足够的权限来运行脚本。 It'll depend on OS you're using. 这取决于您使用的操作系统。

Also you'd consider using CGI module as mentioned in scrappedcola 's answer. 你也想考虑使用CGI模块中提到scrappedcola的答案。 This code will work for both POST and GET: 此代码将适用于POST和GET:

use strict; use warnings;
use CGI;

my $form = CGI->Vars;
print "Content-type: text/html\n\n";
print "name=".$form->{name};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM