I'm totally clueless to the behaviour of the following two pieces of somewhat similar code to upload files to the server:
# code 1 use CGI; my $q = new CGI; my $file = $q->param("upfile"); # $file set to 'C:\images\apple.gif'; # $img_str set to 'apple.gif' my ($img_str) = ($file =~ /.*\\(.*\.(gif|jpg))/); open(OUT,">C:/tmp/$img_str") or die; binmode $file; binmode OUT; my $buffer = ''; while (read($file, $buffer, 1024)) { print OUT $buffer; } close(OUT); # end of use CGI # code 2 use CGI qw(:standard); my $file = param('upfile'); # $file set to 'C:\\images\\apple.gif'; my ($img_str) = ($file =~ /.*\\(.*\.(gif|jpg))/); open(OUT,">C:/tmp/$img_str"); binmode $file; # This line is not present in code 1 with use CGI open(FH,"$file"); binmode FH; binmode OUT; my $buffer = ''; while (read(FH, $buffer, 1024)) { print OUT $buffer; } close(OUT);
1) With use CGI, the name of $file is 'C:\images\apple.gif'. With use CGI qw(:standard), the name of $file is 'C:\\images\\apple.gif'. That is, an additional '\'.
2) With use CGI qw(:standard), there is a need to open (and read) the contents of $file. There's no need for that with use CGI.
Can someone please enlighten me on the differences?
Great thanks in advance :)
Update: As pointed by jeffa, I made a mistake with (2). There's no need to open (and read) the contentsof $file.
Update2: I'm not quite surely actually. Sometimes it works sometimes it doesn't :(
Update3: Stupid me. No need for that read...:) code2 below is a modified version of the one above.
# code 2 use CGI qw(:standard); use File::Basename; my $file = $q->param('upfile') || ''; my $img_str = basename($file); open(OUT,">C:/tmp/$img_str"); binmode $file; binmode OUT; my $buffer = ''; while (read($file, $buffer, 1024)) { print OUT $buffer; } close(OUT);
In reply to use CGI vs use CGI qw(:standard) by kiat
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |