首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法修改逻辑与(&&)标量赋值

无法修改逻辑与(&&)标量赋值
EN

Stack Overflow用户
提问于 2012-04-23 23:26:13
回答 5查看 4.9K关注 0票数 3

当我测试我正在做的CGI表单的构建时,我得到了下面的错误。我正在尝试验证用户是否在CGI表单中输入了数据,以及值是否已提交。我是不是漏掉了什么?

致以敬意,

代码语言:javascript
复制
C:\xampp\cgi-bin>perl create_report.cgi
Can't modify logical and (&&) in scalar assignment at create_report.cgi line 105
, near "'Submitted')"

代码:

代码语言:javascript
复制
if (defined $user_entry && $user_entry='Submitted') {
sub show_form {
    print qq{<form name="input" action="create_report.cgi" method="post">\n};
    print qq{<table align="center" border="1" bordercolor="black" cellpadding='2' cellspacing="0">\n};
    print qq{<tr>};
    print qq{<td align="right">Please enter the description of the Report you  are copying</td};
    print qq{</tr>\n};
    print qq{<tr>};
    print qq{<td align="left"><input type"text" width="7" name="spns"  value=""};
    print qq{<BR>Be sure to use just one or two words to find the report.  Like "Mortgage Summary".</td>};
    print qq{</table><center><input type="submit" value="Submitted"></center></form>\n};
}
EN

回答 5

Stack Overflow用户

发布于 2012-04-23 23:47:44

这里有两个错误。首先,在需要字符串相等运算符(eq)的地方使用赋值运算符(=)。

但其次,你有一个关于优先级的问题。您希望将布尔条件解析为:

代码语言:javascript
复制
(defined $user) && ($entry = 'Submitted')

但实际上,它被解析为:

代码语言:javascript
复制
defined ( ( $user && $entry ) = 'Submitted')

我认为这清楚地说明了为什么你会得到你所看到的错误。

票数 8
EN

Stack Overflow用户

发布于 2012-04-23 23:30:19

如果要使用赋值运算符=检查Perl的值,请使用eq比较Perl中的两个字符串:

代码语言:javascript
复制
if (defined $user_entry && $user_entry eq 'Submitted')
票数 4
EN

Stack Overflow用户

发布于 2012-04-23 23:30:28

您在这里混淆了=赋值和eq字符串等效运算符。使用eq (查看有关perlop中的Perl运算符的更多信息)。将子定义放在if中也不是一个好主意。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10283501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档