4 ms·
When I used to code in Perl (more than ten years ago) I tended to solve many many things using regexes. I tried to reduce many programming challenges into trans
by submeta 9y ago
When I used to code in Perl (more than ten years ago) I tended to solve many many things using regexes. I tried to reduce many programming challenges into transformations that I could do via regexes. Why? Because regular expressions are first class citizens in Perl. And Perl made me learn and use them. (Reminds me of a an APL programmer that saw vectors everywhere.)
When I replaced Perl with Python in my toolbox I abandoned regexes altogether because I didn't know too many Python programmers using regexes.
Now a decade later I am realizing how often I do need regexes in my day to day tasks (using them in bash commands, in my editor, and even in Python sometimes).
- marktangotango 9y agoSome elucidation on this point: https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/ https://blog.codinghorror.com/regular-expressions-now-you-ha...
- submeta 9y agoGreat read. Thanks for sharing.
- brightball 9y agoI found the same thing. When I was writing Perl half the point was using regex. I inherited a site about 5 years ago that was a huge Perl site built entirely on text files and regex. The developer had basically built a database out of text files. He had directories for indexes, associations, etc. It wasn't pretty to look at but it was still to this day the fastest site I've ever worked on.
- tzahola 9y agoHow did he achieve transactionality?
- brightball 9y agoAnywhere that he'd need transaction safety in the system, everything was contained in a single file. There were other jobs that could rebuild indexes by scan, repair associations or break things down into parts of use on other areas of the site but the areas that needed safety were lumped together as a single record (similar to the NoSQL approach honestly). It wasn't a general purpose setup, but it worked for his purposes.
- philipov 9y agoRegex patterns might not be first class citizens in python, but they're easy enough to use, and I think they're ultimately more powerful than in Perl. I don't remember being able to use a callback to calculate my replace string as a function of the match results when doing substitution in Perl. It's super easy to do tokenization with python regexes. We lose first class patterns, but gain first class functions and classes :)
- kqr 9y agoNot to mention that writing a composable first-class API to contruct regexes should be fairly elementary in Python.
- philipov 9y agooh what a good idea... wait, surely someone's already built this, right? like sqlalchemy's expression language, but for regex. If noone's built that yet, I'd build that.
- bmn__ 9y ago> I don't remember being able to use a callback to calculate my replace string as a function of the match results when doing substitution in Perl. That's easy with the `e` (eval) flag. say '13 37 123 42' =~ s { ( \d+ ) # get all the numbers } { my $total += $+; # replace with running total }egrx; __END__ output is 13 50 173 215 > I think they're ultimately more powerful than in Perl No. Perl regex are more powerful than Python because 1. the programmer can pick from a large selection of delimiters 2. the full-featured engine is built-in and ready to use, but in Python you need to install the third-party "regex" module because the built-in one named "re" is hopelessly dyd¹ 3. you may enable verbose (readable) character classes 4. the regex subsystem is pluggable with a common interface and you can within a lexical scope switch at run-time with more restricted implementations which have less features but better performance in some cases: https://metacpan.org/search?q=re%3A%3Aengine%3A%3A https://metacpan.org/search?q=re%3A%3Aengine%3A%3A 5. you can embed arbitrary code with (?{...}) and (??{...}) > We lose first class patterns, but gain first class functions and classes :) Perl had first-class functions since 1993. You love having first-class classes in Python, but do not realise the downside. Python's one true meta object system (with its ageing design from the last millennium) is flawed: horizontal composition of methods with the same name does not emit a warning. Since the system is baked into the language, it cannot be changed. Contrast with Perl, where the language only provides some primitives on which to build a meta object system (incl. first-class classes). This enables bugs to be fixed, and the competition among several implementations on CPAN breeds excellence. ¹ https://i.imgur.com/66z4KDA.jpg https://i.imgur.com/66z4KDA.jpg