http://www.perlmonks.org?node_id=1109947

MidLifeXis has asked for the wisdom of the Perl Monks concerning the following question:

I asked a question on CB today if the benefits of using named capture groups outweigh the overhead, especially from a maintenance view. Given the varied answers and opinions, as well as encouragement to make a post for a wider range of comments, I am posting the question here. Specifically, I am breaking an almost free form logfile format into tokens (FlexLM, for those that are interested). Each line in the log file may be in one of many different formats.

The two general forms I am looking at are...

my $re_foo = qr{ ... (?<type> ... ) ... (?<name1> ... ) }x; my $re_bar = qr{ ... (?<name2> ... ) ... (?<type> ... ) ... }x; my $re_all = qr{$re_foo|$re_bar}; if ( $data =~ $re_all ) { return { %+ }; } ...

vs

my $re_foo = qr{ ... ( # type ... ) ... ( # name1 ... ) }x; my $re_bar = qr{ ... ( # name2 ... ) ... ( # type ... ) ... }x; if ( $data =~ $re_foo ) { return { type => $1, name1 => $2 }; } elsif ( $data =~ $re_bar ) { return { type => $2, name2 => $1 }; } ...

The first seems to me to be much more maintainable, even if performance is impacted a bit. What other opinions, comments, or concerns are there about this construct?

--MidLifeXis