Guest User

Untitled

a guest
Jan 10th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.60 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. sub make_file_chunker {
  6.     my ($fh, $n) = @_;
  7.     my $eof = 0;
  8.     return sub {
  9.        # Don't try to read past EOF.
  10.        # This matters for terminals.
  11.        return if $eof;
  12.  
  13.        my @chunk;
  14.        for (1..$n) {
  15.           my $line = <$fh>;
  16.           if (!defined($line)) {
  17.              $eof = 1;
  18.              last;
  19.           }
  20.  
  21.           chomp($line);
  22.           push @chunk, $line;
  23.        }
  24.  
  25.        return @chunk;
  26.     };
  27. }
  28.  
  29. my $iter = make_file_chunker($fh, 4);
  30. while ( my @chunk = $iter->() ) {
  31.    print(join("\n", @chunk) . "\n\n");
  32. }
Advertisement
Add Comment
Please, Sign In to add comment