5 ms·
Perl's had something like that for a long time: quote operators. You can quote a string using " or ' (which mean different things), and you can quote a regex us
by DougWebb 10y ago
Perl's had something like that for a long time: quote operators. You can quote a string using " or ' (which mean different things), and you can quote a regex using /. But for each of these you can change the quote character by using a quote operator: qq for the double-quote behavior, q for the single-quote behavior, and qr for the regex behavior. (There are a few others two, but I used these most often.)
my $str1 = qq!This is "my" string.!;
my $str2 = qq(Auto-use of matching pairs);
$str2 =~ qr{/url/match/made/easy};
The work I did with Perl included a LOT of url manipulation, so that qr{} syntax was really helpful in avoiding ugly /\/url\/match\/made\/hard/ style escaping.
- leephillips 10y agoThis seems like an awesome feature. I wish Python had something like it.
- masklinn 10y agoPython has triple-quoted strings which generally do the trick, and uses prefixes for "non-standard" string behaviours (though it doesn't have a regex version IIRC, Python 3.6 adds interpolation via f-strings) str1 = f"""This is "my" string.""" str2 = """Auto-use of matching pairs""" str3 = r"""/url/match/made/easy"""
- leephillips 10y agoYes, I've belatedly caught on to using triple-quotes to avoid some escaping. But I didn't know about the f-strings - thanks! (I'll be using those when I start using 3.6.)
- deleted 10y ago[deleted]
- wruza 10y agoPerl also supports heredocs — blocks of full lines with explicit terminator-line: print '-', substr(<<EOT, 0, -1), '!\n'; Hello, World EOT Prints: -Hello, World! iirc sh-shells also have that.
- brennen 10y agoPerl is still, I think, the gold standard for quoting and string manipulation syntax. I am to this day routinely perplexed by the verbosity and ugliness of simple operations on strings in other languages. (Of course, this may also be one of the reasons that programmers in its broad language family have a pronounced tendency to shoehorn too many problems into complex string manipulation, but I suppose no capability comes without its psychological costs.)
- i336_ 10y agoYup, the 8085 CPU emulator in VT102.pl[1] uses a JIT which is essentially a string-replacement engine. [1]: http://cvs.schmorp.de/vt102/vt102 http://cvs.schmorp.de/vt102/vt102 (note - contains VT100 ROM as binary data, but opens in browser as text)