Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Whats your favorite nonstandard regex quote char?

by BrowserUk (Patriarch)
on Apr 22, 2003 at 21:30 UTC ( [id://252368]=note: print w/replies, xml ) Need Help??


in reply to Whats your favorite nonstandard regex quote char?

I almost universally use s[...][...]g; for regexes the exception being when I'm using the /e modifier in which case I tend to use s[...]{....}ge; as the curlies give a visual indication that the right-hand side is active rather than passive. In this specific case, the same regex in my test code is coded as

s[[{].*?[}]][{$sub[$n++]}]g;

which (I think) looks fine under the syntax highlighting in my editor, but looked altogether too confusing when rendered in the b&w of the preview page. So I looked for some way of rendering it more clearly in this environment. I tried various options and settled on

s_\{.*?\}_{$sub[$n++]}_g;

as the least bad.

As you pointed out, perl is very clever about deciding whether a metachar is or is not being used as a metachar, but I still tend to escape them as my brain/eyes are less adept at the art.

I've recently started using [metachar] (unashamedly stolen from some of Abigail-II's posts) instead of \metachar for escaping metachars in regexes as it allows me to use a consistant method for single and multiple alternatives, and I'm finding that consistancy is the key to easy visual parsing of code.

I use m[...] almost universally in preference to /.../, and map{...}; grep{...}; even when the blocking isn't strictly required for similar reasons. I find the visual consistancy and ease of extensibility far outway the minor performance penalty.

I've never yet posted a set of personal style rules as I'm still formulating mine. Very little of my style (or lack thereof) is yet fixed in stone, I tend to see things that other people are doing that seem particularly clear/neat/concise/cool and try them for a while and see what sticks. Those that don't bug me to type, hinder my reading or cause any problems in other ways tend to stay.

There are some which I use in my own code that I relegiously remove from the code when posting. Eg.

.... #! This is a comment

Try as hard as I might to tailor my syntax highlighter definition, it still confuses everything on a line after

$#array

with a comment and highlight it accordingly. So I changed the comment card spec to being #!. This means that all the comments and the shebang lines are displayed in a muted green, which suits me. However, I started removing (mostly) the ! from the comments when posting as a private msg from someone said that everytime he looked at the code I posted with them in, he saw multiple shebag lines and freaked.

I guess I should get around to setting up PerlTidy to filter code to my preferences on input and back to something "more normal" on output, but that probably wouldn't help much as I tend to c&p directly from the editor. I also have a set of macros that do most of the transformations--tab width, curly positioning etc--already in my editor. To use perltidy I would have to save the code out to a file, load it in another editor to view it in the "more normal" form a c&p from there, which would just be a pain.


Examine what is said, not who speaks.
1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
3) Any sufficiently advanced technology is indistinguishable from magic.
Arthur C. Clarke.

Replies are listed 'Best First'.
Re: Re: Whats your favorite nonstandard regex quote char?
by kelan (Deacon) on Apr 23, 2003 at 15:05 UTC

    What editor do you use? I had the same problem with Vim, where it would comment the rest of the line after $#array. I fixed it by changing the perlComment definition to this:

    "$\@<!#.*"
    which only matches a # not preceded by a $.

    kelan


    Perl6 Grammar Student

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found