"single character substitutions are better done with tr///" is not something I have seen documented anywhere. Is this a question of experience, or something I should have found for myself?

Um. Not sure about how I first learned it; probably reading a post hereabouts along time ago.

The thing to note is that tr/// is dedicated to replacing single chars with other single chars; and builds a translation table at compile time. Ie. It does just that one thing and does all the preparations up front.

On the other hand, s/// does all kind of stuff and has to interpret both the input specifications and replacements at runtime; so it is less efficient for this purpose.

When I tried to sort out the backslash following the variable, I think I was trying to put it within the \Q\E part. Is this even possible?

Backslashes pretty much always have to be escaped -- you can get away with them unescaped in single quotes if they don't come in front of a '.

If you put \ inside: \Q\\E, the second backslash escapes the third and the E is just an ordinary E. If you do \Q\\\E, the backslash ends up doubled in the results.

Things line \Q\E, quotemeta and qr are an area where my searches have been pretty fruitless. Are they identical?

\Q\E do the same as quotemeta, but only to that subset of the string or search term to which they are applied.

qr// is a quite different animal that is documented in http://perldoc.perl.org/perlop.html#Regexp-Quote-Like-Operators; and is intended for building regex strings, but turns out not to be as useful as you'd think because what it builds get re-interpreted if you include it as part of a another qr// or m// or s///.

A point on which I was expecting correction was another construct I tried to use without success. I have seen (and cargo culted) something like my ($plsname) = $curdir =~ regex;

In the form you've posted that would assign (the first) capture group in the regex to $plsname; which isn't applicable here.

You could do ( my $plsname = $curdir ) =~ s/.../.../; which would do the assignment, then operated on the new variable; but it's much of a muchness.

I always find it hard to answer 'how would I have learnt that' questions, because I've long since forgotten when/how I learnt them.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Combining regexes by BrowserUk
in thread Combining regexes by davies

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.