首页 > 解决方案 > CGI Perl 在字符串中使用未初始化的值 $user

问题描述

我在检查我用 CGI 检索的变量是否正确时遇到问题。

HTML:

<label for="name">Nombre de Usuario:</label>
<input type="text" id="name" name="user_name" />

<label for="password">Contraseña:</label>
<input type="password" id="password" name="user_password" />

CGI/Perl 代码:

my $c = CGI->new;
.
.
.
my $user = $c->param('user_name');
my $password = $c->param('user_password');

if($user eq "" || $password eq "") {
        printf "Error";
        exit;
} 

主要问题是,如果我这样做:

print $user;

输出是正确的,它打印了值,但是在 apache error.log 中,它仍然说变量未初始化,然后进入“if”并退出。

我还尝试使用“$user”并将字符串与 undef 进行比较,但没有。

标签: perlcgi

解决方案


要测试变量是否具有定义的值,您应该使用“定义”函数:

if (!defined($user) || !defined($password)) { ... }

推荐阅读