为什么我会得到“重新定义” 带有“使用常量”的警告 在 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)
为什么这些行在那里,有办法阻止它们吗?
发出此警告的代码(取自《CGI Programming with Perl》一书,其中包含一些 错误已修复):
#!/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.
我没有放置任何警告 qw/redefine/
但它没有帮助。
I run CGI script with apache2 and I have this warning lines in error.log (I removed all similar lines from the output):
[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;
}
This code have similar output:$ 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/ but it didn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,只有当您修改脚本然后由
mod_perl
为符合内联条件的子例程重新编译脚本时,您才会收到这些警告。 当子例程重新编译时,如果它返回的值发生更改,则该新值将不会反映在先前内联的位置。如果您更改了 BUFFER_SIZE 等值,则应重新启动 apache。
我还认为 mod_perl / Apache::Registry 意外关闭 与您的脚本有关。
AFAIK, you only get these warnings when you modify your script and then the script is re-compiled by
mod_perl
for subroutines that are eligible for inlining. When the subroutine is recompiled, if the the value it returns changed, that new value would not be reflected in the places where it was previously inlined.If you change the value of, say,
BUFFER_SIZE
, you should re-startapache
.I also think mod_perl / Apache::Registry accidental closures is relevant to your script.
据猜测,FOO 的第一个定义正在被优化掉。 在正文中用语句定义它,我想您会发现错误消失了。
<代码>
$ perl -e 'sub FOO () { print 1; } 开始{ *FOO = sub () { 2 }; } 打印 FOO;'
At a guess, the first definition of FOO is being optimized away. Define it with a statement in the body and I think you'll find the error disappears.
$ perl -e 'sub FOO () { print 1; } BEGIN{ *FOO = sub () { 2 }; } print FOO;'