in reply to Is Text::RE::Foo a good name space?

Not a comment on the question, but on the regexp.

/[a-b]|[c-d]|[e-f]/
can be written as a single character class
/[a-bc-de-f]/

Also, it might be easier to specify what's valid and negate the set.
/[\x00-@\[-`]...]/
would become
/[^a-zA-Z...]/
Both are equivalent, but the latter is more readable.

Replies are listed 'Best First'.
Re^2: Is Text::RE::Foo a good name space?
by fernandes (Monk) on Oct 11, 2007 at 18:04 UTC
    Probably your mother language is English. It is low frequency language on comma occurrences. Then, your spell out module – this is a chomskian expression - process [a-bc-d] “more” productively than [a-b]|[c-d]. But, try to think out of your box, and you will can see that it is not necessary so. To me, what you gain in reducing length, suppressing OR operators, you loose in readability. Any way, thanks for the free wisdom.

      I must admit I realized it was less readable as I was posting. I wonder if there's a significant speed difference, since this regexp is likely to be used repeatedly. Either way, the regex could be built dynamically when the module is loaded. That would allow for much more readable code than any of the alternatives we've seen in this thread.

        alternations are slower, but tr is faster for simple expressions:
                       Rate alternate    single        tr
        alternate  631309/s        --      -80%      -86%
        single    3198780/s      407%        --      -27%
        tr        4411077/s      599%       38%        --
        
        use Benchmark qw(cmpthese); cmpthese(-1, { alternate => \&alternate, single => \&single, tr => \&tr_op }); my $s; BEGIN{ $s = 'this is a string'; } sub alternate { return $s =~ /[a-b]|[c-d]|[e-f]/ ; } sub single { return $s =~ /[a-f]/; } sub tr_op { return $s =~ tr/a-f/a-f/; }