Conserving memory - was SPUG: Fw: greetings

Ken Clarke kenslinux at home.com
Sat Oct 20 16:18:05 CDT 2001


Hi Folks,

    I love picking up efficiency tips like the one at the bottom.  Which
context does print use?

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

Thanks.

>> Ken Clarke
>> Contract Web Programmer / E-commerce Technologist
>> www.perlprogrammer.net


> | while ($k = <FILE>) { ... }
>
> Which is exactly right; because <FILE> is being read in a scalar context
this
> time, it only returns one line at a time.  That's gonna save you a lot of
RAM,
> and therefore swapping time.
>
> This is a fairly common gotcha that hits everyone from time to time.
Implied
> contexts can be confusing.  If you try to remember what *should* be in a
> particular location, you'll know how the <...> operator works.  For
example:
>
>         while( <FILE> ) { ... }
>
> will read one line at a time, but
>
>         for( <FILE> ) { ... }
>
> will read the entire file up front, and then process it.
>
> Hope that helps.
>
>  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>      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/
>
>
>


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     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