[Melbourne-pm] Memory usage and sending files

Paul Fenwick pjf at perltraining.com.au
Mon Jun 19 20:34:15 PDT 2006


G'day everyone,

While we're optimising the optimisations, I thought a discussion of the 
following snippet may be useful:

> while (<PDFFILE>) {
> 	print;
> }

One thing to be careful of with the code above is that until you've 
altered $/, then it will be operating on a line-by-line basis.  In a 
PDF, or any other non-text file, those some of those lines may be quite 
long, so you may not be getting the memory savings you expect.

Luckily, Perl's File::Copy can be used to copy files to filehandles:

	use File::Copy qw(copy);

	binmode(STDOUT);
	copy($filename, \*STDOUT) or die "Cannot copy - $!";

File::Copy slurps the entire file, or uses a 2Mb buffer, whichever is 
smaller.  If you want to be particular about the buffer-size, then you 
can even supply a third argument to copy if you want to be particular 
about the buffer-size.

	use File::Copy qw(copy);
	use POSIX qw(BUFSIZ);

	binmode(STDOUT);
	copy($filename, \*STDOUT, BUFSIZ) or die "Cannot copy - $!";

Cheerio,

	Paul

-- 
Paul Fenwick <pjf at perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681


More information about the Melbourne-pm mailing list