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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Named captures or positional variables
by tobyink (Canon) on Dec 11, 2014 at 11:21 UTC | |
Re: Named captures or positional variables
by LanX (Saint) on Dec 10, 2014 at 21:04 UTC | |
by RonW (Parson) on Dec 11, 2014 at 00:34 UTC | |
by LanX (Saint) on Dec 11, 2014 at 01:18 UTC | |
by tobyink (Canon) on Dec 11, 2014 at 11:25 UTC | |
by Anonymous Monk on Dec 11, 2014 at 01:14 UTC | |
Re: Named captures or positional variables
by Anonymous Monk on Dec 10, 2014 at 20:34 UTC | |
by MidLifeXis (Monsignor) on Dec 10, 2014 at 20:44 UTC | |
Re: Named captures or positional variables
by RonW (Parson) on Dec 10, 2014 at 20:54 UTC |