perl - 为什么我在 mod_perl 下收到 "redefine"警告和 "use constant"?

标签 perl cgi warnings mod-perl

我使用 apache2 运行 CGI 脚本,并且在 error.log 中有此警告行(我从输出中删除了所有类似的行):

[Thu Jul 30 09:39:37 2009] upload.pl: Constant subroutine ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::UPLOAD_DIR redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133.
Constant subroutine
    ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::BUFFER_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133 (#1)
[Thu Jul 30 09:39:37 2009] upload.pl: Constant subroutine ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::BUFFER_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133.
Constant subroutine
    ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_FILE_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133 (#1)
[Thu Jul 30 09:39:37 2009] upload.pl: Constant subroutine ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_FILE_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133.
Constant subroutine
    ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_DIR_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133 (#1)
[Thu Jul 30 09:39:37 2009] upload.pl: Constant subroutine ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_DIR_SIZE redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133.
Constant subroutine
    ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_OPEN_TRIES redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133 (#1)
[Thu Jul 30 09:39:37 2009] upload.pl: Constant subroutine ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::MAX_OPEN_TRIES redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133.
Subroutine dir_size redefined at /home/stanislav/cgi/perl/upload.pl line 79,
     line 133 (#2)
[Thu Jul 30 09:39:37 2009] upload.pl: Subroutine dir_size redefined at /home/stanislav/cgi/perl/upload.pl line 79,  line 133.
Subroutine error redefined at /home/stanislav/cgi/perl/upload.pl line 90,
     line 133 (#2)
[Thu Jul 30 09:39:37 2009] upload.pl: Subroutine error redefined at /home/stanislav/cgi/perl/upload.pl line 90,  line 133.
Argument "" isn't numeric in numeric ge (>=) at
    /home/stanislav/cgi/perl/upload.pl line 62 (#4)
[Thu Jul 30 09:39:37 2009] -e: Argument "" isn't numeric in numeric ge (>=) at /home/stanislav/cgi/perl/upload.pl line 62.
Filehandle OUTPUT opened only for input at /home/stanislav/cgi/perl/upload.pl
    line 69 (#5)
[Thu Jul 30 09:39:37 2009] -e: Filehandle OUTPUT opened only for input at /home/stanislav/cgi/perl/upload.pl line 69.
Constant subroutine
    ModPerl::ROOT::ModPerl::Registry::home_stanislav_cgi_perl_upload_2epl::UPLOAD_DIR redefined at /usr/share/perl/5.10/constant.pm line 115,  line 133 (#1)

Why this lines are there and is there a way to stop them?

Code that makes this warnings (taken from book "CGI Programming with Perl", with some bugs fixed):



#!/usr/bin/perl
use strict;
use warnings;


use CGI;
use CGI::Carp;
#use diagnostics qw/-verbose/;

use Fcntl qw( :DEFAULT :flock );
use constant UPLOAD_DIR     => "/tmp/test_upload/";
use constant BUFFER_SIZE    => 16_384;
use constant MAX_FILE_SIZE  => 1_048_576;       # Limit each upload to 1 MB
use constant MAX_DIR_SIZE   => 100 * 1_048_576; # Limit total uploads to 100 MB
use constant MAX_OPEN_TRIES => 100;

$CGI::DISABLE_UPLOADS   = 0;
$CGI::POST_MAX          = MAX_FILE_SIZE;
my $q = new CGI;
$q->cgi_error and error( $q, "Error transferring file: " . $q->cgi_error );
my $file      = $q->param( "file" )     || error( $q, "No file received." );
my $filename  = $q->param( "filename" ) || error( $q, "No filename entered." );
my $fh        = $q->upload( "file" )     || error( $q, "Something is wrong with file handle." );
#my $fh        = $q->upload( $file );
my $buffer    = "";
if ( dir_size( UPLOAD_DIR ) + $ENV{CONTENT_LENGTH} > MAX_DIR_SIZE ) {
    error( $q, "Upload directory is full." );
}
# Allow letters, digits, periods, underscores, dashes
# Convert anything else to an underscore
$filename =~ s/[^\w.-]/_/g;
if ( $filename =~ /^(\w[\w.-]*)/ ) {
    $filename = $1;
}
else {
    error( $q, "Invalid file name; files must start with a letter or number." );
}
# Open output file, making sure the name is unique
until ( sysopen OUTPUT, UPLOAD_DIR . $filename, O_CREAT | O_EXCL ) {
    $filename =~ s/(\d*)(\.\w+)$/($1||0) + 1 . $2/e;
    $1 >= MAX_OPEN_TRIES and error( $q, "Unable to save your file." );
}
# This is necessary for non-Unix systems; does nothing on Unix
binmode $fh;
binmode OUTPUT;
# Write contents to output file
while ( read( $fh, $buffer, BUFFER_SIZE ) ) {
    print OUTPUT $buffer;
}
close OUTPUT;

if ( -T $fh ) {
    print $q->header("text/plain");
    seek $fh, 0, 0;
    map { print } ;
}

sub dir_size {
    my $dir = shift;
    my $dir_size = 0;

    # Loop through files and sum the sizes; doesn't descend down subdirs
    opendir DIR, $dir or die "Unable to open $dir: $!";
    while ( $_ = readdir DIR ) {
        $dir_size += -s "$dir/$_";
    }
    return $dir_size;
}
sub error {
    my( $q, $reason ) = @_;

    print $q->header( "text/html" ),
          $q->start_html( "Error" ),
          $q->h1( "Error" ),
          $q->p( "Your upload was not procesed because the following error ",
                 "occured: " ),
          $q->p( $q->i( $reason ) ),
          $q->end_html;
    exit;
}


此代码具有类似的输出:$ perl -e 'sub FOO () { 1 } BEGIN{ *FOO = sub () { 2 }; } print FOO;'
Constant subroutine main::FOO redefined at -e line 1.
I did put no warnings qw/redefine/但它没有帮助。

最佳答案

AFAIK,只有在修改脚本然后脚本由 mod_perl 重新编译时才会收到这些警告。对于符合内联条件的子程序。当子例程被重新编译时,如果它返回的值发生了变化,那么这个新值将不会反射(reflect)在它之前被内联的地方。

如果您更改的值,例如 BUFFER_SIZE ,你应该重新开始apache .

我也觉得mod_perl / Apache::Registry accidental closures与您的脚本相关。

关于perl - 为什么我在 mod_perl 下收到 "redefine"警告和 "use constant"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1205116/

相关文章:

maven - 如何找到请求丢失 POM 的 POM?

perl - Base64 正确解码为 blob?

php - 在 Perl 中循环数组构建多维数组,从 PHP 转换,语法错误

perl - 现在不再支持 $* 如何用替代方案替换 $*=1

Perl:从子例程返回后哈希值发生变化

c - 摆脱 sscanf 中的警告

javascript - perl cgi 脚本无法发布到站点 - 独立工作但在 apache 下运行时失败

javascript - 为什么需要 8 个反斜杠?

python - 如何使用python从http header 在服务器中执行bash命令?

python - PyBrain 弃用警告