*星期日的第二次更新*
现在,我已经在显示所需的 sub 时找到了一些额外的成功 新的代码片段使我能够按照自己的意愿实际调用一个 sub 。
在研究中,我偶然发现了以下片段,其中涉及阅读传入的 FORM 数据。 此代码段 从此脚本启用我选择的 sub 的调用。 但是,当我对脚本运行 perl -x 时,CLI会返回以下内容 nonfatal *警告*我希望了解并解决。 我的研究表明(tr ///)和 $ ENV {" REQUEST_METHOD"} 和 $ buffer 返回空 值"" OR 0 。 我如何才能最好地解决以下这些错误? 我意识到我可以删除任何 引用 (tr ///) 和 $ buffer 来解决这些错误,但是,我提出了删除问题 * $ ENV {" REQUEST_METHOD"} * 因为看起来这个片段的功能势在必行???
CLI错误
Use of uninitialized value in transliteration (tr///) at test.pl line 36 (#1) Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at test.pl line 37 (#1) Use of uninitialized value $buffer in split at test.pl line 44 (#1)
#!/usr/bin/perl -w
# (test.pl)
use DBI;
use DBD::mysql;
use warnings;
use strict;
use diagnostics;
$| = 1;
# The script I am wanting to create, is to allow users at (NAS) HotSpot to create a user account
# which is to write into MySQL db TABLE's *radcheck* and *radreply*.
#
# Now at this point I have found some added success at displaying the desired *sub*
# The new snippet of code which enabled me to actually *invoke* a specific *sub* as I wanted
# from an HTML form.
# Please see below for solution which still has some questions.
print "Content-type: text/html\n\n";
sub BuildAcctNow {
print "<h1 style=\"color:blue;font-family:Arial;font-size:xx-large;\">TO BUILD YOUR ACCOUNT TODAY WE WILL NEED A SMALL AMOUNT OF INFORMATION</h1><br><br>\n\n";
}
sub PauseAcctNow {
print "<h2 style=\"color:red;font-family:Arial;font-size:xx-large;\">YOUR ACCOUNT HAS BEEN PAUSED PLEASE MAKE A PAYMENT HERE.</h2><br><br>\n\n";
}
# In researching I stumbled upon the fllowing snippet which deals with reading inward FORM data.
# This snippet *does* enable the *invocation* of the *sub* of my choice from this script.
# However from the CLI when I run perl -x against the script the system returns the following
# *nonfatal* *warnings* that I would like to gain understading of and resolve.
# My research shows that (tr///) and $ENV{"REQUEST_METHOD"} and $buffer are returning empty
# values, How would I best resolve these following errors? I realize I can just delete any
# reference to (tr///) and $buffer to resolve those errors, howerver I question removing
# $ENV{"REQUEST_METHOD"} as it seems this imperative to the function of th ssnippet???
#
#
# Use of uninitialized value in transliteration (tr///) at test.pl line 36 (#1)
# Use of uninitialized value $ENV{"REQUEST_METHOD"} in string eq at test.pl line 37 (#1)
# Use of uninitialized value $buffer in split at test.pl line 44 (#1)
my ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read in text
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
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;
}
if ($FORM{PauseAcct}) {
PauseAcctNow();
exit;
}
elsif ($FORM{BuildAcct}) {
BuildAcctNow();
exit;
}
结束第二个星期日更新
SUNDAY UPDATE ** 我已经制作了一个简单的脚本,希望能够有希望地展示我想要做的事情吗? 我最终需要创建的脚本将写入MySQL db到 radcheck 和 radreply ,以使用户能够登录到(NAS)HotSpot。 所以我将在脚本中有多个子程序。
当我在名为 BuildAcct 的文档中使用带有正确命名的SUBMIT表单的HTML文档时,脚本当前显示一个空屏幕VIA浏览器。
我传统上习惯于在脚本中定义 sub 然后我会在脚本中定义 if test(s)来等待匹配任何已定义的表单名称与它们进行交互,然后调用特定的 sub 。
下面是我创建的测试脚本,试图通过过时的使用&amp;在调用 sub 时,这让我有些悲伤,我希望得到一些有价值的信息。
#!/usr/bin/perl -w
# (test.pl)
use DBI;
use DBD::mysql;
use warnings;
#use strict;
# Presently due to my errors below I have disabled *use strict*.
$| = 1;
# The script I am wanting to create, is to allow users at (NAS) HotSpot to create a user account
# which is to write into MySQL db TABLE's *radcheck* and *radreply*.
#
# Trying to bring myself up to speed with a very basic task which I have defined below, in my
# older scripts I would define the *sub* itself, then in the script I would use an *if* test
# which checks to see if any defined FORM value returns a hit such as $form_data{'BuildAcct'} ne ""
# to call the required *sub* _ThisOne_.
print "Content-type: text/plain\n\n";
sub ThisOne {
print "Trying to display this subroutine upon submission of BuildAcct\n";
}
# Following is the *if* test I am accustomed to using to make a call to a particular sub when
# the form NAME BuildAcct is interacted with, but this is unacceptable now I realize.
# CLI Return:
# Use of uninitialized value $form_data{"BuildAcct"} in string ne at test.pl line 32.
# Use of uninitialized value $form_data{"BuildAcct"} in string ne at test.pl line 41.
if ($form_data{'BuildAcct'} ne "")
{
&ThisOne;
exit;
}
# SO, I have Google'd, and looked over numerous methods of calling *subs*, I am just stuck though,
# Why can't the following *if* test work if use of & is no longer used?
if ($form_data{'BuildAcct'} ne "")
{
ThisOne();
exit;
}
提前感谢您的帮助...... 最诚挚的问候
更新**
我已经在脚本上关闭了-w开关,不确定这是否会产生负面影响,这对于perl来说是个新手。
我还创建了一些丑陋的笨重代码。 奇怪的是,当我执行脚本时,CLI从系统返回:
Use of uninitialized value $form_data{"BuildAcct"} in string at acctmanager.pl line 211.
Use of uninitialized value $form_data{"Test"} in string at acctmanager.pl line 212.
然而,来自HTML文档的VIA浏览器我可以在 BuildAcct 和 Test 之间来回更改 SUBMIT 名称值,并且脚本成功返回提交时有两个不同且正确的子程序。
BuildAcct 子返回我在该子例程中定义的表单字段,而 Test 执行MySQL TABLE GROUP rowfetch并显示来自db的3个不同的表并将它们打印到浏览器。
以下是我现在的代码: - (
local ($form_data{'BuildAcct'}) = "$form_data{'BuildAcct'}";
local ($form_data{'Test'}) = "$form_data{'Test'}";
#
# AddNewUser FORM definition.
if ($form_data{'BuildAcct'} ne "")
{
&AddNewUser;
exit;
}
#
# DispTest FORM definition.
elsif ($form_data{'Test'} ne "")
{
&DispTest;
exit;
}
有人可能会给我一个正确的推动方向吗?
提前感谢你
ORIGINAL POST
此时我在一个名为 BuildAcct 的HTML文档上有一个FORM,同样在我的脚本中我定义了以下内容来调用子例程 AddNewUser 当用户提交HTML FORM ...
时 if ($form_data{'BuildAcct'} ne "")
{
&AddNewUser;
exit;
}
该脚本使用 cgi-lib.pl
# Enable parsing of FORM_DATA VIA cgi-lib.pl.
&ReadParse(*form_data);
## FORM or IMG FORM Fix
foreach (keys %form_data)
{
## Fix incoming form data with Image buttons
$form_data{$1} = $form_data{$_} if (/(.*)\.x/);
}
我无法理解的是为什么这在我使用的另一个脚本中有效,但是这个新脚本在CLI执行时会返回以下内容;
Use of uninitialized value $form_data{"BuildAcct"} in string ne at acctmanager.pl line 208.
Use of uninitialized value $form_data{"Test"} in string ne at acctmanager.pl line 215.
非常感谢帮助和建议。 最诚挚的问候
答案 0 :(得分:1)
我最好的猜测是通过cgi-lib.pl填充%form_data
哈希值,因此当您通过命令行运行它时,cgi-lib.pl没有从Web浏览器获取任何输入。说过这个,你没有把代码包含在你使用cgi-lib.pl的地方,所以我不能确定。
P.S。 不关闭警告。他们是有原因的。如果没有别的,您可以将警告粘贴到谷歌。当你在它的时候,总是把use strict;
放在脚本的顶部,并修复所有未声明的变量。