Hi monks,

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);
The differences are as follows:

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.