简体   繁体   中英

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 . Prepend a $ to it in the print and the 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. But you have provided Bareword - OUTFILE_FOTO in the program .You can try to avoid these errors by using 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM