user3547496
user3547496

Reputation: 1

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);

Upvotes: 0

Views: 55

Answers (2)

Praveen
Praveen

Reputation: 902

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);

Upvotes: 1

Jim Davis
Jim Davis

Reputation: 5290

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 theprint and the close.

Upvotes: 2

Related Questions