[英]Why is this Perl IO code not working?
Why does this code not work: 为什么此代码不起作用:
my $file_handle = $cgi->upload('file');
open my $OUTFILE_FOTO, '>>', "image/$new_file.$extension";
while (<$file_handle>) {
print OUTFILE_FOTO $_;
}
close($file_handle);
close(OUTFILE_FOTO);
but this code does work: 但是这段代码确实有效:
my $file_handle = $cgi->upload('file');
open( OUTFILE_FOTO, '>>', "image/$new_file.$extension" );
while (<$file_handle>) {
print OUTFILE_FOTO $_;
}
close($file_handle);
close(OUTFILE_FOTO);
In the first example, you're declaring and assigning a scalar $OUTFILE_FOTO
, but you're referring to it later as if it were a bareword OUTFILE_FOTO
. 在第一个示例中,您声明并分配了标量
$OUTFILE_FOTO
,但是稍后要引用它,就好像它是一个裸OUTFILE_FOTO
OUTFILE_FOTO OUTFILE_FOTO
。 Prepend a $
to it in the print
and the close
. 在
print
和close
时在其前面加上$
。
This should work now . 现在应该可以使用了。 You have created a file handle variable when you see my $OUTFILE_FOTO and this is one of the way to implement perl best practises.
看到我的$ OUTFILE_FOTO时,您已经创建了一个文件句柄变量,这是实现perl最佳实践的方法之一。 But you have provided Bareword - OUTFILE_FOTO in the program .You can try to avoid these errors by using use strict;
但是您已经在程序中提供了Bareword-OUTFILE_FOTO。您可以尝试使用use strict来避免这些错误; and use warnings;
并使用警告;
my $file_handle = $cgi->upload('file');
open my $OUTFILE_FOTO, '>>', "image/$new_file.$extension";
while (<$file_handle>) {
print $OUTFILE_FOTO $_;
}
close($file_handle);
close($OUTFILE_FOTO);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.