in reply to reading several lines in a gulp

my @lines; push @lines, scalar <$file> for 1..10;

No need for a primitive for something that can be easily achieved with existing primitives, and isn't used all that often.

Perl buffers the data it reads from the file, so it shouldn't be much less efficient than slurping a small file in list context.

Replies are listed 'Best First'.
Re^2: reading several lines in a gulp
by John M. Dlugosz (Monsignor) on Apr 27, 2011 at 12:31 UTC
    If the file is not a multiple of 10 lines, does that push extra undef's into the @lines?
      Yes, I didn't think of that. Maybe this would be better:
      sub gulp { my ($file, $count) = @_; my @lines; for (1..$count) { push @lines, scalar <$file>; last if eof $file; } return @lines; }
Re^2: reading several lines in a gulp
by raybies (Chaplain) on Apr 27, 2011 at 12:20 UTC

    Why is scalar used in that? What's the effect? I'm staring crosseyed at this very useful solution, can someone decompress this one for me?

    Thanks ahead of time for all you experts and your great solutions...

    --Ray

      Why is scalar used in that? What's the effect?

      push imposes list context (you can push more than one element in one statement), and <$file> would read the whole file at once in list context (as pointed out in the OP).

      In other words, without scalar, the whole file would be read in the first iteration, which would kind of defeat the purpose of the exercise...