On Tue, Nov 10, 2009 at 14:11, Paul LeoNerd Evans <leon...@leonerd.org.uk> wrote: > After some headscratching I decided instead to have parse() return a > list of the capture groups. I so far haven't found a neater expression > than > > > sub parse > { > my ( $text, $re ) = @_; > $_[0] =~ s/^$re// or die "Expected $re in $text...\n"; > > return map { substr $text, $-[$_], $+[$_]-$-[$_] } 1 .. $#+ > } > > > This seems a common-enough idiom that perhaps there's a neater solution > - I find there's no @{^MATCHGROUPS} or similar present in perl... > > Can anyone offer any neater suggestions?
For matches, you can use list context assignment, which will give you the groups. Unfortunately, that doesn't work for substitutions, which always return a count of substitutions made. But you could try this: sub parse { my ( $text, $re ) = @_; my @matches = $_[0] =~ /^$re// or die "Expected $re in $text...\n"; $_[0] =~ s/^$re//; return @matches } at the cost of running the regexp twice (once for matching and capturing, then once for substituting). Cheers, Philip -- Philip Newton <philip.new...@gmail.com>