Perl脚本使用-w开关,但不是没有

rot*_*cke 0 perl warnings switch-statement

此脚本适用于带有-w交换机的localhost,但不是没有.当它也可以use strictuse warning活跃.

的Apache2/error.log中:

没有开关(中止脚本):

(2)No such file or directory: exec of ... failed
Run Code Online (Sandbox Code Playgroud)

我得到的开关:

Use of uninitialized value $email_flag in string ne ...
Run Code Online (Sandbox Code Playgroud)

看起来很像我.

在实时Web服务器上都没有人工作.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";
Run Code Online (Sandbox Code Playgroud)

Dav*_*idO 7

您是否在Windows计算机上创建脚本并将其上载到Linux服务器而不修复行结尾?如果没有-w开关,shebang线可能看起来像" #!/usr/bin/perl\r",因此系统会寻找名为"perl\r"的程序(或者行结束外观).使用-w开关时," #!/usr/bin/perl"没有难以理解的行结尾.相反,它会被卡在-w不会导致失败的地方.

我认为有一个关于这个的perlfaq,但我现在似乎无法在文档中找到它.

更新:我在PerlMonks上找到了它,在一个非常古老的问答主题中看起来似乎无关,直到你阅读了消息正文:答案:如何摆脱脚本标题的过早结束.是的,我知道,如果你只是浏览线程,你甚至不会停止那个.但这是帖子的文字:

如果您在Windows上开发此脚本,则脚本文件可能具有非UNIX行结尾.(perl解释器可以处理它们,但是shebang行由shell解释,并且不能容忍错误的行结尾.)如果这是问题,脚本可能会在shebang行处以错误终止.