I have the strangest problem I've seen so far in Perl. Please let someone explain what is going wrong here:
The case is simple. I have a file to upload via HTTP (you know: upload button, use CGI and get the file). I've rewritten the original script to this:
#!/usr/bin/perl -w
use strict;
use CGI;
$|=1;
my $buffer;
print "Content-type: text/html\n\n";
print "<html><body>";
my $req = CGI->new;
my $filename = $req->param("filename");
$filename =~ m/(.*?$CGI::SL)*(.*)(?=$)/;
my $localfilename = "$2";
open(FILE, ">$localfilename") || print "Could not open $localfilename
+for output: $!<br>\n";
while (my $bytesread = read($filename,$buffer,1024))
{
print FILE $buffer;
}
close(FILE);
print "File saved as $localfilename (original filename=$filename)<br>"
+;
print "</body></html>";
the HTML file looks like this (most simple form of course)
<html>
<body>
<form method=POST encoding="multipart/form-data" action="http://blur/n
+ewtest.cgi">
<table border=1>
<tr><td>Filename: </td><td><input type=file name="filename"></td></tr>
<tr><td colspan=2><input type=submit></td></tr>
</table>
</form>
</body>
</html>
When I run this code and select any file, the moment the 'open' is called, the value of
$localfilename is erased. Originally I used
FileHandle but to make sure the error wasn't in that module, I replaced it with the plain
open statement.
Now the strange part: when I leave out the
use strict; everything works fine and the value of
$localfilename is preserved.
I'm at a new job here, and everyone here tells me 'use strict' is no good, and I'm trying to convince them of the opposite, 'cause I always use it, and really believe anyone should use it, but this is making my statement a bit hard to defend....
Jouke Visser, Perl 'Adept'
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.