# return ($1, $2, ...) matched against $s
sub _groups {
my $s = shift;
my @groups;
foreach my $i (1..$#-) {
push @groups, substr($s, $-[$i], $+[$i] - $-[$i]);
}
return @groups
}
####
if (/$re/mgc) {
@groups = _groups($_); # ($1, $2, ...)
}
####
Title: The Moor's Last Sigh
Author: Salman Rushdie
Publisher: Foo
Title: The God of Small Things
Author: Arundhati Roy
Publisher: Bar
####
my $text = THE EXAMPLE TEXT ABOVE ...
my $re_title = qr/Title: (.*?)$/;
my $re_author = qr/Author: (\w+) (\w+)$/;
my $re_publisher = qr/Publisher: (.*?)$/;
my @answers;
{
my %book;
if ($text =~ /$re_title/mgc) {
$book{title} = $1;
}
if ($text =~ /$re_author/mgc) {
$book{author} = [ $1, $2 ];
}
if ($text =~ /$re_publisher/mgc) {
$book{publisher} = $1;
}
push @answers, \%book;
}
{
my %book;
if ($text =~ /$re_title/mgc) {
$book{title} = $1;
}
...
####
if (@groups = $text =~ /Title: (.*?)$/mgc) {
$book{title} = $1;
}