Created
April 2, 2012 20:18
-
-
Save hoehrmann/2286936 to your computer and use it in GitHub Desktop.
read_line_group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!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