Previous Page
Next Page

12.6. Lazy Flags

Consider mandating the Regexp::Autoflags module.

It takes about a week to accustom your fingers to automatically typing /xms at the end of every regex. But, realistically, some programmers will still not have the discipline required to develop and foster that good habit.

An alternative is to allow (that is, require) them to use the Regexp::Autoflags CPAN module instead, at the start of every source file they create. That module will then automatically turn on /xms mode in every regex they write.

That is, if they put:

    use Regexp::Autoflags;

at the start of their file, from that point on they can write regexes like:

    $text =~ m{\A          
# From start of string...
# ...match and capture any characters (including newlines!)
^_ _END_ _$
# ...until the first _  _END_  _ line


    $source_code =~ s{               
# Substitute...
# ...a literal octothorpe
# ...followed by any number of non-newlines
} {$SPACE}g;
# Replacing it with a single space

They won't have to remember to append the all-important /xms flags, because the Regexp::Autoflags module will have automatically applied them.

Of course, this merely replaces the need for one kind of discipline (always use /xms) with the requirement for another (always use Regexp::Autoflags). However, it's much easier to check whether a single module has been loaded at least once, than it is to verify that the right regex flags have been used everywhere.

    Previous Page
    Next Page