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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|