Conserving memory - was SPUG: Fw: greetings

Tim Maher/CONSULTIX tim at consultix-inc.com
Sat Oct 20 16:30:53 CDT 2001


On Sat, Oct 20, 2001 at 02:18:05PM -0700, Ken Clarke wrote:
> Hi Folks,
> 
>     I love picking up efficiency tips like the one at the bottom.  Which
> context does print use?

print, like all functions (and subroutines), provides the LIST
context to its arguments.

> 
> # Start returning results to client browser
> $| = 1;
> open(FH, "<templates/RealTop.html");
> print "Content-Type: text/html\n\n";
> print <FH>;

That approach stores the entire file in memory (to no avail;
as you show below, an equivalent result can be obtained reading
just one line at a time).

> close(FH);
> 
> or should I be using:
> 
> open(FH, "<templates/RealTop.htm");
> print "Content-Type: text/html\n\n";
> while ($line = <FH>) {
>     print "$line\n";
> }
> >> Ken Clarke
> >> Contract Web Programmer / E-commerce Technologist
> >> www.perlprogrammer.net

For efficiency, this is definitely the way to  go, but
the while loop is better written as:

	 while ( defined ($line = <FH>) ) {

-Tim
*=========================================================================*
| Dr. Tim Maher, CEO, Consultix        (206) 781-UNIX/8649;  ask for FAX# |
| EMAIL: tim at consultix-inc.com         WEB: http://www.consultix-inc.com  |
| TIM MAHER: UNIX/Perl  DAMIAN CONWAY: OO Perl  COLIN MEYER: Perl CGI/DBI |
|CLASSES:Int Perl 10/22; UNIX 11/26; Minimal Perl 11/30; Perl+Modules 12/3|
| /etc/cotd:  find /earth -follow -name bin-laden -print | xargs rm -rf   |
*=========================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://zipcon.net/spug/





More information about the spug-list mailing list