2 ms·
> 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
by 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