in reply to Re: Strange behaviour of tr function in case the set1 is supplied by a variable
in thread Strange behaviour of tr function in case the set1 is supplied by a variable
you're guessing how tr/// works, you're guessing it works like s/// or m///, but you can't guess , it doesn't work like that, it doesn't interpolate variables, read perldoc -f tr for the detailsHouston, we have a problem ;-)
First of all that limits tr area of applicability.
The second, it's not that I am guessing, I just (wrongly) extrapolated regex behavior on tr, as people more often use regex then tr. Funny, but searching my old code and comments in it is clear that I remembered (probably discovered the hard way, not by reading the documentation ;-) this nuance several years ago. Not now. Completely forgotten. Erased from memory. And that tells you something about Perl complexity (actually tr is not that frequently used by most programmers, especially for counting characters).
And that's a real situation, that we face with Perl in other areas too (and not only with Perl): Perl exceeds typical human memory capacity to hold the information about the language. That's why we need "crutches" like strict.
You simply can't remember all the nuances of more then a dozen of string-related built-in functions, can you? You probably can (and should) for index/rindex and substr, but that's about it.
So here are two problems here:
1. Are / / strings uniformly interpreted in the language, or there is a "gotcha" because they are differently interpreted by tr (essentially as a single quoted strings) and regex (as double quoted strings) ?
2. If so, what is the quality of warnings about this gotcha? There is no warning issued, if you use strict and warnings. BTW,it looks like $ can be escaped:
main::(-e:1): 0 DB<5> $_='\$bba\$' DB<6> tr/\$/?/ DB<7> print $_ \?bba\?
Right now there is zero warnings issued with use strict and use warnings enabled. Looks like this idea of using =~ for tr was not so good, after all. Regular syntax like tr(set1, set2) would be much better. But it's to late to change and now we need warnings to be implemented.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Strange behaviour of tr function in case the set1 is supplied by a variable
by QM (Parson) on Nov 16, 2017 at 09:45 UTC | |
by likbez (Sexton) on Nov 16, 2017 at 20:05 UTC | |
by likbez (Sexton) on Nov 16, 2017 at 22:39 UTC | |
|
Re^3: Strange behaviour of tr function in case the set1 is supplied by a variable
by Anonymous Monk on Nov 16, 2017 at 09:40 UTC | |
by likbez (Sexton) on Nov 16, 2017 at 22:56 UTC |