in reply to Re^2: Combining regexes
in thread Combining regexes
"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.
|
|---|