in reply to file handling

Use $. ($INPUT_LINE_NUMBER) from perlvar in a while loop:
while (<>) { print if $. > 2; }

Replies are listed 'Best First'.
Re^2: file handling
by EvanCarroll (Chaplain) on Jul 09, 2008 at 16:37 UTC
    using IO::File
    use IO::File; my $fh = IO::File->new( 'foobar.txt', 'r' ); while ( my $line = $fh->readline ) { print $line if $fh->input_line_number > 3; }


    Evan Carroll
    I hack for the ladies.
    www.EvanCarroll.com
      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.

        That's because the method is called getline. :)

        while ( my $line = $fh->getline ) {
Re^2: file handling
by blazar (Canon) on Jul 16, 2008 at 17:05 UTC

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

    --
    If you can't understand the incipit, then please check the IPB Campaign.