Re: Why does this CGI process hang?
by Newton, Philip other posts by this author
Aug 28 2001 6:55AM messages near this date
view in the new Beta List Site
Perl locale problems
|
Re: Why does this CGI process hang?
Kamphuys, ing. K.G. wrote:
> But do you have any idea how to get the contents of the uploaded
> file available? As the documentation states, the filename (here
> in $input{file}) also serves as a file handle
True. The version of CGI.pm I have has this in the documentation, however:
: When the form is processed, you can retrieve the entered filename by
: calling param():
:
: $filename = $query-> param('uploaded_file');
:
: Different browsers will return slightly different things for the name.
: Some browsers return the filename only. Others return the full path to
: the file, using the path conventions of the user's machine. Regardless,
: the name returned is always the name of the file on the *user's*
: machine, and is unrelated to the name of the temporary file that CGI.pm
: creates during upload spooling (see below).
:
: The filename returned is also a file handle. You can read the contents
: of the file using standard Perl file reading calls:
:
: # Read a text file and print it out
: while (<$filename> ) {
: print;
: }
:
: # Copy a binary file to somewhere safe
: open (OUTFILE,"> >/usr/local/web/users/feedback");
: while ($bytesread=read($filename,$buffer,1024)) {
: print OUTFILE $buffer;
: }
:
: However, there are problems with the dual nature of the upload fields.
: If you `use strict', then Perl will complain when you try to use a
: string as a filehandle. You can get around this by placing the file
: reading code in a block containing the `no strict' pragma. More
: seriously, it is possible for the remote user to type garbage into the
: upload field, in which case what you get from param() is not a
: filehandle at all, but a string.
:
: To be safe, use the *upload()* function (new in version 2.47). When
: called with the name of an upload field, *upload()* returns a
: filehandle, or undef if the parameter is not a valid filehandle.
:
: $fh = $query-> upload('uploaded_file');
: while (<$fh> ) {
: print;
: }
:
: This is the recommended idiom.
So you will get problems if you "use strict" (which you're already doing,
right?). So if you have at least CGI.pm v2.47, you could consider moving to
the upload function, which gives you a "real" filehandle rather than a
string which can be used like one but complains under "strict refs".
> and that was, as far as I can understand, what I did. TIA.
No, you didn't. It's subtle, however. You did:
> while (<$input{file}>) {
Here's what `perldoc perlop` says, under "I/O Operators":
: If angle brackets contain is a simple scalar variable (e.g., <$foo> ),
: then that variable contains the name of the filehandle to input from, or
: its typeglob, or a reference to the same. For example:
:
: $fh = \*STDIN;
: $line = <$fh> ;
:
: If what's within the angle brackets is neither a filehandle nor a simple
: scalar variable containing a filehandle name, typeglob, or typeglob
: reference, it is interpreted as a filename pattern to be globbed, and
: either a list of filenames or the next filename in the list is returned,
: depending on context. This distinction is determined on syntactic
: grounds alone. That means `<$x> ' is always a readline() from an indirect
: handle, but `<$hash{key}> ' is always a glob(). That's because $x is a
: simple scalar variable, but `$hash{key}' is not--it's a hash element.
So what you did was a file glob that looked for all files that matched the
name of the uploaded file instead.
If you want to treat what you got as a filehandle, you either need to assign
$input{$file} to a simple scalar variable (like in the CGI.pm examples), or
call readline() explicitly (`perldoc -f readline` and `perldoc perlop` for
more information).
And in any case, remember to use binmode both on the input file and the
output file if you could possibly be dealing with non-text files, since
you're on a Win32 system.
Cheers,
Philip
--
Philip Newton <Philip.Newton@[...].de>
All opinions are my own, not my employer's.
If you're not part of the solution, you're part of the precipitate.
_______________________________________________
Perl-Win32-Web mailing list
Perl-Win32-Web@[...].com
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-web
Thread:
Newton, Philip
Marcelo A. Oliveto
|