0

* 周日第二次更新*

现在在这一点上,我发现在显示所需的sub方面取得了一些额外的成功 。新的代码片段使我能够根据需要实际调用特定的sub

在研究中,我偶然发现了以下处理读取传入FORM数据的片段。这个片段确实可以调用我从这个脚本中选择的。但是,当我对脚本 运行perl -x时,系统会从 CLI返回以下非致命的*警告*,我想了解和解决这些问题。我的研究表明 (tr///)$ENV{"REQUEST_METHOD"}$buffer正在返回空值"" OR 0。我将如何最好地解决以下这些错误?我意识到我可以删除任何对(tr///)$buffer来解决这些错误,但是,我质疑删除 *$ENV{"REQUEST_METHOD"}*因为这似乎对这个片段的功能是必要的???

命令行错误

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;
  }

第二个星期日更新结束

周日更新** 我制作了一个简单的脚本,希望能展示我目前正在尝试做的事情?我最终需要创建的脚本将写入 MySQL db 到radcheckradreply中,以使用户能够登录(NAS)HotSpot。所以我将在脚本中有多个子例程。

当我在名为BuildAcct的文档中使用带有正确命名的 SUBMIT 表单的 HTML 文档时,该脚本目前显示一个空白屏幕 VIA 浏览器。

我传统上习惯于在脚本中定义,然后我会在脚本中定义if测试,它会在与任何定义的表单名称交互时等待匹配,然后调用特定的

下面是我创建的一个测试脚本,只是为了在调用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 浏览器我可以在BuildAcctTest之间来回更改SUBMIT名称值,并且脚本在提交时成功返回两个不同且正确的子例程。

BuildAcct子程序返回我在该子例程中定义的表单字段,而Test执行MySQL TABLE GROUP 行提取并显示来自 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 ,同样在我的脚本中,我定义了以下内容,即当用户提交 HTML FORM 时调用子例程AddNewUser ...

 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.

非常感谢您的帮助和建议。此致

4

2 回答 2

1

我最好的猜测是%form_data哈希是通过 cgi-lib.pl 填充的,因此当你通过命令行运行它时,cgi-lib.pl 没有从网络浏览器获得任何输入......话虽如此,你还没有t 包括您使用 cgi-lib.pl 的代码,所以我不能确定。

ps不要关闭警告。他们在那里是有原因的。如果不出意外,您可以将警告粘贴到谷歌。当您使用它时,请始终将其放在use strict;脚本的顶部,并修复所有未声明的变量。

于 2013-04-21T00:36:39.077 回答
0

要修复错误“在字符串中使用未初始化的值 $form_data{"BuildAcct"}”,

在使用 $form_data{"BuildAcct"} 之前,您可以使用如下所示的 defined($form_data{"BuildAcct"}) 或定义的 $form_data{"BuildAcct"} 进行检查:

if (defined $form_data{"BuildAcct"}) { <其他代码基于$form_data{"BuildAcct"} > }

于 2021-11-10T12:10:45.353 回答