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)

In reply to Re: Meaning of command by Bethany
in thread Meaning of command by grewal7634

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.