use IO::File;
my $fh = IO::File->new( 'foobar.txt', 'r' );
while ( my $line = $fh->readline ) {
print $line if $fh->input_line_number > 3;
}
| [reply] [d/l] |
I like the different approach. But, I can not get your example to work. I get this error:
Can't locate object method "readline" via package "IO::File"
Admittedly, I am running an older version of perl (5.8.5 on linux), but if I look at the docs for the more recent versions of perl (5.8.8 and 5.10.0) for the IO::File core module, they also do not mention a "readline" method. Does your code actually run for you? Just curious.
| [reply] [d/l] |
| [reply] [d/l] [select] |
I personally believe that there are basically two approaches:
- one as yours, which involves doing something unnecessary -namely the check- even when it will boil down to a noop, and is aesthetically unsatisfying even if the actual impact on performance will be negligible;
- one which will skip two lines (if there are!) in advance and then proceed as usual, and is also aesthetically unsatisfying for its inherent unavoidable code duplication.
This kind of situation (as opposed to very specific one) has led me in the past to many "doing it only once" kinda questions both in 5 and 6 realms. (The search is suboptimal, BTW, but should shed some light.) Eventually, for the most common case, TimToady just said that as far as Perl 6 is concerned, it may be a matter of compiler optimization behind the curtains, and that's fine for me. Whatever, in Perl 5 instead I can't help but feeling that there's something missing, however small, negligible, unimportant the practical problem may be. (But that's just me, I know...)
(Apologies for replying so late.)
| [reply] [d/l] |