【问题标题】:Perl script works with -w switch but not withoutPerl 脚本可与 -w 开关一起使用,但并非没有
【发布时间】:2011-06-11 01:09:47
【问题描述】:

这个脚本在带有-w 开关的本地主机上工作,但不是没有。当use strictuse warning 处于活动状态时,它也可以工作。

apache2/error.log:

没有开关(中止脚本):

(2)No such file or directory: exec of ... failed

用我得到的开关:

Use of uninitialized value $email_flag in string ne ...

在我看来是初始化的。

在实时网络服务器上,没有一个工作。 Perl 对我来说是新的,但我知道一些 BASH 和 PHP。

我运行 Debian Lenny、Apache2、Perl 5.10。

#!/usr/bin/perl -w

$| = 1;

my $mailprog = '/usr/sbin/sendmail'; # where the mail program lives

my $to = "not\@for.you";   # where the mail is sent

my ($command,$email,@pairs,$buffer,$pair,$email_flag) ;

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

        @pairs = split(/&/, $buffer);
       foreach $pair (@pairs) {

        # Split the pair up into individual variables.                       #
        my($name, $value) = split(/=/, $pair);

        # Decode the form encoding on the name and value variables.          #
        $name =~ tr/+/ /;
        $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        $value =~ tr/+/ /;
        $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

        # If they try to include server side includes, erase them, so they
        # aren't a security risk if the html gets returned.  Another 
        # security hole plugged up.
        $value =~ s/<!--(.|\n)*-->//g;

     ##  print "Name of form element is $name with value of $value \n";

        if ($name eq 'email') {
            $email = $value;
           }

        if ($name eq 'command') {
            $command = $value;
           }

       }

    if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
        $email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/ ) {
        $email_flag = "ERROR";

    }

my $urlcommand = $command;


if ($command eq 'Subscribe') {
$command = "SUBSCRIBE rpc-news";
}

if ($command eq 'Unsubscribe') {
$command = "UNSUBSCRIBE rpc-news";
}

if ($command eq 'Suspend') {
$command = "SET rpc-news NOMAIL";
}

if ($command eq 'Resume') {
$command = "SET rpc-news MAIL";
}

my $getInfo = '';

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

  if ($email_flag ne "ERROR") {

    open(MAIL,"|$mailprog -t");
     print MAIL "To: $to\n";
     print MAIL "From: $email\n";
     print MAIL "Subject: [rpc-news] $command \n";
     print MAIL "Reply-to: $email \n";

     print MAIL "$command \n";
     print MAIL "EXIT \n";
    close (MAIL);

    $getInfo = "?result=good";
   }
if ($email_flag eq "ERROR") {
    $getInfo = "?result=bad";
}   

my $rootURL= $ENV{'SERVER_NAME'};
my $url = "http://${rootURL}/thank_you.html${getInfo}&action=${urlcommand}";

print "Location: $url\n\n";

【问题讨论】:

  • 不确定为什么代码的格式不适用于整个脚本。
  • 因为您必须选择所有代码并应用代码格式。您使用的标记仅在一行代码中有用,代码块使用间距来格式化。
  • 查看How do I format my code blocks?获取格式化帮助

标签: perl warnings switch-statement


【解决方案1】:

您是否在 Windows 机器上创建脚本并将其上传到 Linux 服务器而不修复行尾?如果没有 -w 开关,shebang 行可能看起来像“#!/usr/bin/perl\r”,因此系统会寻找一个名为“perl\r”的程序(或者行尾看起来如何)。使用 -w 开关,“#!/usr/bin/perl”不会有难以辨认的行结尾粘在上面。相反,它会被困在 -w 不会导致失败的地方。

我以为有关于此的 perlfaq,但目前我似乎无法在文档中找到它。

更新:我在 PerlMonks 上找到了它,在一个非常古老的问答主题中,在您阅读消息正文之前似乎无关紧要:Answer: How to get rid of premature end of script headers。是的,我知道,如果您只是浏览线程,您甚至不会停下来。但这是帖子的正文:

如果您在 Windows,脚本可能 文件具有非 UNIX 行结尾。 (这 perl 解释器可以处理它们,但是 shebang 线由 外壳,并且不容忍 不正确的行尾。)如果这是 问题,脚本可能会终止 在shebang出现错误 行。

【讨论】:

  • 确实,原始文件是在 Windows 上使用 Dreamweaver 编辑的。我现在使用 kwrite 在 debian 上工作,但 cat -A 显示 #!/usr/bin/perl^M$
  • dos2unix 解决了这个问题。 debian 包被命名为: tofrodos 。非常感谢。
【解决方案2】:

Use of uninitialized value $email_flag in string ne ...

在我看来它已经初始化了。

 if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||
     $email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/
 ) {
     $email_flag = "ERROR"; 
 }

$email_flag 仅在模式匹配时才在此处初始化 - 否则未定义。您可以添加一个else 子句以确保它无论如何都会被初始化。

【讨论】:

  • 或者定义并初始化它:my $email_flag = "";(显然,将它与当前同时定义的其他变量分开)。
  • my ($command,$email,@pairs,$buffer,$pair,$email_flag) ; 我认为顶部的排列初始化了 $email_flag
  • @mario - 该行不初始化变量,它只是声明它们,将它们的值保留为 undef。
【解决方案3】:

我不会使用该代码,它不使用 CGI.pm(或 CGI::Simple ...) 从“nms - 专家编写的网络程序”中获取“TFMail - 改进的表单邮件”

它安装简单,而且写得很好(它使用 CGI ...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    相关资源
    最近更新 更多