<?xml version="1.0" encoding="windows-1252"?>
<node id="771344" title="Regexp Legibility" created="2009-06-14 01:21:59" updated="2009-06-14 01:21:59">
<type id="11">
note</type>
<author id="771335">
patrickhaller</author>
<data>
<field name="doctext">
I used to (before use strict) compose regexps by hiding the components inside an if, e.g.:

&lt;code&gt;
$complex_re = /^($ip) ($host) ($msg)$/ if (
    $ip   = /\d+\.\d+\.\d+\.\d+/,
    $host = /[\-\.\w]+/,
    $msg  = /.*/
);
&lt;/code&gt;

Nowadays, I use strict and eval, e.g. 

&lt;code&gt;
my $complex_re = eval {
    my $ip   = qr/\d+\.\d+\.\d+\.\d+/;
    my $host = qr/[\-\.\w]+/;
    my $msg  = qr/.*/;

    return /^($ip) ($host) ($msg)$/;
);
&lt;/code&gt;

We pay about a 5% penalty for the eval when we use this in a tight loop, however we can solve that by moving the regexp creation outside the loop.

&lt;code&gt;
                 Rate    with eval without eval
with eval    116279/s           --          -5%
without eval 121951/s           5%           --
&lt;/code&gt;


Patrick</field>
<field name="root_node">
383015</field>
<field name="parent_node">
383015</field>
</data>
</node>
