Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I'm testing the following using Perl 5.24.0.
Observe the strange behavior of "tr" when single-quotes are used as delimiters:
Input:
#!/usr/bin/perl -w -- $s = '-abcdefgh'; $s =~ tr/abc//cd; print "s = '$s'\n"; $s = '-abcdefgh'; $s =~ tr/a-c//cd; print "s = '$s'\n"; $s = '-abcdefgh'; $s =~ tr'abc''cd; print "s = '$s'\n"; $s = '-abcdefgh'; $s =~ tr'a-c''cd; print "s = '$s'\n";
Output:
s = 'abc' s = 'abc' s = 'abc' s = '-ac'
Effectively, character ranges get disabled when this specific character is used as the delimiter, and "-" becomes just one more literal character in the SEARCHLIST. Is this outcome intended? Did I miss where this special case is documented? I would expect an exception like that to be listed in the doc (tr leads you to Quote Like Operators; scroll down a bit to the part on tr and y).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: undocumented tr special behavior for single-quoted transform
by LanX (Saint) on Jan 06, 2018 at 10:33 UTC | |
by Anonymous Monk on Jan 06, 2018 at 10:45 UTC | |
by LanX (Saint) on Jan 06, 2018 at 11:20 UTC |