[lug] perl question

Hugh Brown hugh at math.byu.edu
Sun Apr 9 21:26:55 MDT 2000


> 
> Seek is what you're looking for if you want to do random access to
> a file. If you just want to read it twice a reopening of the file
> might be ok (unless you high speed is an issue).

I had to do that in one other instance.  I was more looking for
information about what other options were.  Good to know the speed
issue.  I have been more of an administrator than a coder (i.e. I fiddle
with config files until things work)  I haven't yet ventured into the
optimization of my fiddling yet.


> Ah, you get your $filename from L. Steins CGI module ...
> CGI does a bit of black magic here (have a look at
> CGI.pm, esp. the 'to_filehandle' function). You can use
> the return value of this function both as a string or
> as a file handle. Since the FileHandle object is inherits
> from IO::File and IO::Seekable you can use the methods of
> these modules to reset the file position.

Good to know (re IO::File and IO::Seekable).  The whole script was from
L. Stein.  I just adapted it.


> of course, in a real life script you would check the
> name of the file, wouldn't you? :-)
> Someone might send you a file with the name 'file.txt; rm -rf /*'

This is a password protected family website (I trust my family, but yes
you are right about the security).  I am in process of making this
script all I want it to be.  I wanted to make sure I could get the file
there before I started customizing further.

> As a matter of fact, your aproach might consume a lot of memory;
> The while(<FD>) construct will read a line at a time, where line
> is defined as 'everything to the next input-record-separator',
> usually a newline. If someone sends you a huge image file then
> your script will probably load quite a lot of data in one bite.
> If you run under Apache's mod_perl this would be deadly (perl
> doesn't give back memory to the OS ...).
> A construct like:
>  open (OUTFILE,">>/usr/local/web/users/feedback");
>  while ($bytesread=read($filename,$buffer,1024)) {
>    print OUTFILE $buffer;
>  }
> from the CGI docs seems more healthy.

This was the next step after verifying the upload.  Plus the while(<FD>)
would barf on non-text files.

> 
> Hope this helped
> 
>   Ralf


Very much so, thank you.

Hugh




More information about the LUG mailing list