Quoting vakeel ahmad ([EMAIL PROTECTED]):
> You can do by this way
> 
> #!/usr/bin/perl
> 
> print "Content-Type:image/gif\n\n";
> 
> open(FILE,"filename");
> @file=(<FILE>);
> foreach $file(@file)
> {
> print "$file";
> }
> 
> close(FILE);

The unnecessary "" symbols in:

print "$file";

are bad style, and may make execution a tiny bit slower. It should be:

print $file;

Since print can take an array argument, it is cleaner and faster to do

print @file;

instead of the foreach { } loop. By combining this with the previous line,
you can do away with the temporary variable, ie.

print <FILE>;

Perl is still forced to break the file up at every linefeed character and
allocate a separate chunk of memory for each. By putting

undef $/;

before the print this wasted effort can be avoided.

If I was writing this for work I would include error checking and read and
write the file in 64k blocks, but there's no fun way to do that in Perl so
I'll leave it out here.

Here's my finished version:

#!/usr/bin/perl

print "Content-type: image/gif\n\n";
open(FILE,"filename);
binmode(FILE);
undef $/;
print <FILE>;


I added the required space to the Content-type: declaration as mentioned by
David Henderson, and added binmode() for broken OSs as suggested by Csaba
Ráduly. I left off the close(FILE) to shave another millionth of a second
off the execution time :-)

Adam

-- 
Adam Rice -- [EMAIL PROTECTED] -- Blackburn, Lancashire, England

Reply via email to