[sf-perl] RE oddity

Quinn Weaver qw at sf.pm.org
Wed Feb 15 12:59:35 PST 2006

On Wed, Feb 15, 2006 at 11:10:08AM -0800, Rich Morin wrote:
> Actually, I'm not sure how Perl could act as I want here,
> while dealing correctly with sharp signs in character
> classes.  So, let's refocus the question:
>   How should this code be written, in order to be fast
>   and well-documented?

    # Punt weird characters.
    $line =~ s|[\000-\010\012-\037\177-\377']||g;
    #            ^^^^^^^  ^^^^^^^^ ^^^^^^^^^
    #            nul-bs    nl-us    del-... and '

You can omit the ^^^^'s if you wish, but I find they help direct my
eyes to the right part of the regex.

In addition, I would probably expand "nl", "bs", and "us" to something more
verbose.  You could take an additional line to do it:

    # Punt weird characters.
    $line =~ s|[\000-\010\012-\037\177-\377']||g;
    #            ^^^^^^^  ^^^^^^^^ ^^^^^^^ ^
    #            nul-      nl-      del-   ' (single quote)
    #            backspace whatever ...,

Here I took the further liberty of breaking out the single quote into
its own comment "field".

Finally, I would change "Punt" to "Ignore", since not everyone
understands US football metaphors.

Hope this helps.  I spend a lot of time trying to optimize for
maintainability, so I have lots of opinions on the issue. ;)

PS:  If you're interested in optimizing for maintainability,
Damian's _Perl Best Practices_ is a great place to start.

qw (Quinn Weaver); #President, San Francisco Perl Mongers
=for information, visit http://sf.pm.org/weblog =cut

More information about the SanFrancisco-pm mailing list