Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Trying to avoid line noise (brain cramp)

by clemburg (Curate)
on Mar 09, 2001 at 18:38 UTC ( #63245=note: print w/replies, xml ) Need Help??


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.

      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.

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

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

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://63245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (1)
As of 2022-10-01 15:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My preferred way to holiday/vacation is:











    Results (1 votes). Check out past polls.

    Notices?