Conserving memory - was SPUG: Fw: greetings

Matt Tucker tuck at
Sun Oct 21 07:08:48 CDT 2001

-- "Tim Maher/CONSULTIX" <tim at> spake thusly:

>> or should I be using:
>> open(FH, "<templates/RealTop.htm");
>> print "Content-Type: text/html\n\n";
>> while ($line = <FH>) {
>>     print "$line\n";

This should be:

     print $line;

to avoid getting doubled newlines. But then, no-one's actually going to
write this code anyway, when a simple:

     system qw(cat templates/RealTop.htm);

will do.

>> }
>> >> Ken Clarke
>> >> Contract Web Programmer / E-commerce Technologist
>> >>
> For efficiency, this is definitely the way to  go, but
> the while loop is better written as:
> 	 while ( defined ($line = <FH>) ) {

I almost said this the last time the subject came up, but then I
couldn't think of a case in which "scalar <FH>" might return a false
value before EOF, since the delimiter is always included, and even a
blank line will be "\n".

I just did a bit more reading, and came up with a few:

    - The last line of the file has no trailing newline
    - $/ is undef, and the file is empty (always returns '' the
      first time)
    - The delimiter is '0', and two such occur together in the file

While these aren't really very common cases in which the true/defined
distinction is relevant, it's probably better to be safe since it's one
less rule to have to think about.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 232 bytes
Desc: not available
Url :

More information about the spug-list mailing list