4 ms·
One thing I still like about Perl is how well the regex syntax is integrated into the language, combined with being installed in just about every Linux modern L
by nineteen999 7y ago
One thing I still like about Perl is how well the regex syntax is integrated into the language, combined with being installed in just about every Linux modern Linux system,
makes it really accessible from your fingertips.
If you're in a hurry and just need to do some one-off slicing and dicing, there's not much more powerful than
being able to say, eg:
$data = "foobarfoo";
$data =~ s/bar/baz/g;
print "$data\n";
versus python's more dramatic and formal:
import re
data = "foobarfoo"
data = re.sub('bar', 'baz', data)
print data
For some reason, the Perl =~ notation sticks in my head
much more easily than the Python method.
Of course you can string a bunch of sed commands together in a shell script as well, but I find that becomes unwieldy pretty quickly in a lot of situations.
- megaframe 7y agoAgreed. In other languages a part of me always feels like if I'm using regex I'm doing something wrong, because I used it so liberally in Perl. It feels like a Perl-ism so I try to make a conscious effort not to carry those over as they tend to be the source of inefficiencies.
- masklinn 7y agoTBF for this specific case it's very much unnecessary to break out regexes: most languages do have some sort of literal replacement string API: str.replace = replace(...) S.replace(old, new[, count]) -> str Return a copy of S with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. (though the documentation turns out more misleading than re.sub's: this states it replaces all occurrences but really replaces all non-overlapping occurrences, which re.sub actually spells out properly).
- megaframe 7y agoOddly enough I was asked once to determine if there were any differences between Perl Regex and Python's implementation in an effort to convert a tool I wrote into Python (so other developers could help) I found only one very weird edge case worth noting, in Perl the regex "s/ a* / x /g" (spaces added to prevent formatting) will turn the string "bac" into "xbxxcx" because the "a" technically means zero or more so it matches the spaces between the characters, not so in python it creates the string "xbxcx" because it matched the a in between one time and didn't count the empty spaces. Slightly less accurate results since * does mean zero or more so the space between counts as zero characters.
- asicsp 7y agoThat was changed recently, I get 'xbxcx' on 3.5 and 'xbxxcx' on 3.7 And coming to Perl vs Python regex differences, there are too many to count, Python's 3rd party module 'regex' is more comparable to Perl regex. For example, Python doesn't support possesive quantifiers, subexpression calls, \K, \G and so on
- interfixus 7y agoA question of taste, disposition and habituation, supposedly. I far prefer the Python way for clarity and immediate intelligibility, no magic spells required.
- throwawayjava 7y agoI much prefer the Perl way for writing short disposable stuff and much the Python way if any other person (including me in more than 1-2 weeks) is going to use/modify/read the code. The gap between perl and python seems cleaner than the gap between python and anything-statically-typed, at least.
- arunix 7y agoSince Perl 5.14, it could be simplified to $data = "foobarfoo" say $data =~ s/bar/baz/gr say is like print, but adds a newline as well. The /r option does a non destructive replace and returns the result.