Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Meaning of command

by Bethany (Scribe)
on Jul 10, 2014 at 20:35 UTC ( [id://1093119]=note: print w/replies, xml ) Need Help??


in reply to Meaning of command

This info might be useful to a beginner. If you aren't used to Perl's way of doing things, the square brackets in $A =~ tr[\n][]d; can look confusingly like array subscripting or references to an anonymous list. Regex operators can be tricky because you get to pick the delimiter. This tripped me up a few times when I was new to Perl.

While reading up on regex operations, pay special attention to how delimiters are chosen. The slash character / is conventionally used in documentation and examples, but you can choose some other non-whitespace character -- or a matching pair of "bracketing" characters, such as [ and ] in your example, or ( and ), et cetera.

Whichever delimiter(s) you choose, if that character or characters has special meaning within regular expressions, and you need to use the character's special meaning as well as its literal value, you'll have to backslash-escape it or them.

For instance, when using [ and ] as delimiters for an expression, within that expression you won't be able to use the square brackets as regex character-set operators unless you escape them with a backslashes. In other words, where you might use /[a-zA-Z][0-9]+/ to match a letter followed by one or more digits, if you choose to use square brackets instead of slashes you'll need to write [\[a-ZA-Z\]\[0-9\]+].

You can see square brackets would be a poor choice of delimiters for that particular regex, since escaping adds "noise" characters that obscure the meaning of the regex. The usual forward slash delimiters, as in the original /[a-zA-Z][0-9]+/, make it easier to tell what the regex does. Since there are no literal slashes in the regex, there's no reason to avoid using / as the delimiter.

However, if you were matching parts of a path or of a URL, either of which is likely to contain slashes as literal characters to match against, square brackets might make sense. Suppose I have a variable containing a path, such as $path_with_fname = "/home/bethany/images/lolcat.gif", and I want to separate the path itself and the filename. I could match fname against [(.*)/(.*?)]. This is less "noisy" than writing /(.*)\/(.*?)/, where the literal slash in the middle must be backslash-escaped.

In other words, try to choose a delimiter character or characters that you won't be using as a literal character within the regex.

I hope this wasn't too confusing, too detailed, or both. Experiment with a throw-away script and various patterns and you'll get the hang of it soon enough.

(edited 'cos typos and grammatical errors will sneak in even when you preview)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2024-04-18 12:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found