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. > if( $line =~ /$searchstring/i ) { > my $trimmed_line = substr( $line , 0 , 60 ); > $trimmed_line =~ s/^\s*//; ## NOTE: Possible logic bug; > ## $trimmed_line now may be < 60 chars > > printf "%s:%s:\t%s\n" , $filename , $. , $trimmed_line; > } > } > close( $INPUT ); > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/