in reply to Trying to avoid line noise (brain cramp)

Avoiding line noise in Perl is mainly about how to handle regexes, IMHO.

A good tip here is to use variables in the regexes:

# original: /([^(]+)(\([^)]+\))(.*)/ my $not_paren = "[^(]+"; my $paren = "\\("; my $rest = ".*"; # now reads: $string =~ /($not_paren)($paren$not_paren$paren)($rest)/;

This will do a good job to make the code readable to other programmers, in my experience.

Christian Lemburg
Brainbench MVP for Perl
http://www.brainbench.com

Replies are listed 'Best First'.
Re: Re: Trying to avoid line noise (brain cramp)
by Ovid (Cardinal) on Mar 09, 2001 at 18:45 UTC
    I like your code, but there's a small error:
    # original: /([^(]+)(\([^)]+\))(.*)/ my $not_paren = "[^(]+"; my $leftparen = "\\("; my $rightparen = "\\)"; my $rest = ".*"; # now reads: $string =~ /($not_paren)($leftparen$not_paren$rightparen)($rest)/;

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Ah, yes, sorry. So much for not testing everything.

      Christian Lemburg
      Brainbench MVP for Perl
      http://www.brainbench.com

      Here's one more way to de-line-noise-ify the code:
      my $leftparen = quotemeta "("; my $rightparen = quotemeta ")";
      This is more legible than "\\(" or "\Q(\E". -- Frag.
Re: Re: Trying to avoid line noise (brain cramp)
by BrotherAde (Pilgrim) on Mar 09, 2001 at 18:53 UTC
    On the same subject, don't forget about the "x"-modifier for regexen. It basically allows you to split a regex into multiple lines and comment on every line.

    Allow me to quote from perlman:perlfaq6, Section 1


    /x lets you turn this:

    s{<(?:[^>'"]*|".*?"|'.*?')+>}{}gs;
    into this:

    s{ < # opening angle bracket (?: # Non-backreffing grouping paren [^>'"] * # 0 or more things that are neither > nor +' nor " | # or else ".*?" # a section between double quotes (stingy +match) | # or else '.*?' # a section between single quotes (stingy +match) ) + # all occurring one or more times > # closing angle bracket }{}gsx; # replace with nothing, i.e. delete
    It's still not quite so clear as prose, but it is very useful for describing the meaning of each part of the pattern.

    Again, hope that helps,
    Brother Ade