Skip to content

Instantly share code, notes, and snippets.

@hoehrmann
Created April 2, 2012 20:18
Show Gist options
  • Save hoehrmann/2286936 to your computer and use it in GitHub Desktop.
Save hoehrmann/2286936 to your computer and use it in GitHub Desktop.
read_line_group
#!perl -w
use strict;
use warnings;
use IO::Unread 'unread';
use Data::Dumper;
sub read_line_group {
my ($handle, $key_regex) = @_;
my $previous_key;
my @lines;
while (<>) {
my ($current_key) = $_ =~ $key_regex;
if (defined $previous_key and $previous_key ne $current_key) {
unread($handle, $_);
last;
}
push @lines, $_;
$previous_key //= $current_key;
}
return @lines;
}
# read two blocks
print Dumper [ read_line_group \*STDIN, qr/^(\S+)/ ];
print Dumper [ read_line_group \*STDIN, qr/^(\S+)/ ];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment