On Feb 12, 2013, at 3:33 PM, Jim Gibson <[email protected]> wrote:

> 
> On Feb 12, 2013, at 3:18 PM, John SJ Anderson wrote:
> 
>> 
>> There's nothing idiomatic about that. I'd write that code as:
>> 
>> # do whatever needed to get filename out of $_ into $filename here
>> open( my $INPUT , '<' , $filename ) or die( "Can't open $filename ($!)" );
>> foreach my $line ( <INPUT> ) {
> 
> I am guessing that you would actually write that with a while loop instead of 
> a foreach loop (and put the dollar-sign in front of INPUT):
> 
>   while( my $line = <$INPUT> ) {
> 
> For the foreach loop, Perl at one time would have to read in the entire file, 
> form a list from the lines, and then iterate over that list, at a big cost in 
> memory for large files. That may have been improved in recent Perls, but 
> unless you know for sure, sticking with the while() loop, which reads lines 
> one-at-a-time, is safer.
> 
> You will also want to add the line:
> 
>   chomp($line);
> 
> If the line is less than 60 characters, a newline character will be left at 
> the end of the line. If it is greater than 60, it will be removed. So if you 
> have any short lines in the input, you will get extra blank lines in your 
> output.

Oh, man, did I leave off my "code is untested and written completely off the 
top of my head" disclaimer? It seems I did. 8^)

Thanks for the corrections, Jim. Honestly, my fingers typed 'while' initially, 
but I flipped it back to a 'foreach' to better match what was there -- I should 
have noted that in a comment. The lack of a sigil on INPUT was just silliness. 

I'm going to claim the lack of chomp() was also to match the code that was 
provided ... that's my story, I'm sticking to it. 8^)

thanks,
john.

-- 
John SJ Anderson // [email protected]



--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/


Reply via email to