Perl 脚本可以使用 -w 开关,但不能没有

发布于 2024-11-14 20:35:54 字数 2805 浏览 2 评论 0原文

该脚本可以在带有 -w 开关的 localhost 上运行,但也不能没有。当 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";

This script works on localhost with the -w switch but not without. It also works when use strict and use warning are active.

apache2/error.log:

without switch (aborted script):

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

with the switch I get:

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

which looks initialised to me.

On the live web server neither one works. Perl is new to me, but I know some BASH and PHP.

I run 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";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静谧幽蓝 2024-11-21 20:35:55

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

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

更新:我在 PerlMonks 上发现了它,在一个非常古老的问答主题中,在您阅读消息正文之前,该主题似乎无关: 答案:如何摆脱脚本头的过早结束。是的,我知道,如果您只是浏览线程,您甚至不会在该线程上停留。但这是帖子的正文:

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

Did you create your script on a Windows machine and upload it to a Linux server without fixing the line endings? Without the -w switch, the shebang line may look like "#!/usr/bin/perl\r", so the system goes looking for a program named "perl\r" (or however the line ending looks). With the -w switch, "#!/usr/bin/perl" doesn't have an indecipherable line ending stuck to it. Instead, that gets stuck to -w where it doesn't cause failure.

I thought there was a perlfaq about this, but I can't seem to find it in the docs at the moment.

Update: I found it over on PerlMonks, in a really old Q&A topic that seems unrelated until you read the body of the message: Answer: How to get rid of premature end of script headers. Yeah, I know, if you were just browsing threads you wouldn't even stop on that one. But here's the text of the post:

If you developed this script on
Windows, it's possible that the script
file has non-UNIX line endings. (The
perl interpreter can handle them, but
the shebang line is interpreted by the
shell, and is not tolerant of
incorrect line endings.) If this is
the problem, the script may terminate
with an error right at the shebang
line.

2024-11-21 20:35:55

在字符串 ne 中使用未初始化的值 $email_flag ...

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

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

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

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

which looks initialised to me.

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

$email_flag only gets initialized here if the pattern matches - otherwise it's left undefined. You could add an else clause to ensure it gets initialized no matter what.

只为一人 2024-11-21 20:35:55

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

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

I would not use that code, it doesn't use CGI.pm (or CGI::Simple ...)
Get "TFMail -- Improved Form Mail" from "nms - web programs written by experts"

Its simple to install, and its written well ( it uses CGI ...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文