3 ms·
Incidentally, I'm working on a language comparison which includes Perl and Python and I would be interested to see how Perl string handling is superior. Did you
by jergosh 15y ago
Incidentally, I'm working on a language comparison which includes Perl and Python and I would be interested to see how Perl string handling is superior. Did you mean speed or syntax or both? I always thought that the Perl way is quite friendly if you have Unix background, but since Python does everything by function calls it's also fine.
As for exception handling, I guess it'll always be shorter in Perl since it doesn't really support exceptions...
- kamaal 15y agoPerl has exception handling, its only that its not available via your usual try-catch syntax. If you want a more sugary exception handling syntax there is always Try::Tiny ( http://search.cpan.org/~doy/Try-Tiny-0.09/lib/Try/Tiny.pm http://search.cpan.org/~doy/Try-Tiny-0.09/lib/Try/Tiny.pm). Another advantage Perl has over Python in case of parsing is that regular expressions are first class elements in Perl, so they can be passed around like objects. Combine them with more powerful things like given-when(http://perldoc.perl.org/perlsyn.html#Switch-statements http://perldoc.perl.org/perlsyn.html#Switch-statements) and parsing becomes a lot more easier when compared to Python. Smart matching is a great feature. And all this obviously leads to better performance in terms of speed because less code to process means (to an extent faster code). And syntactically as well. Imagine doing such a thing in Python. There isn't smart match operator at the first place, there is no switch statement, and on top of that dealing with innumerable regular expression functions(compile,match,search) with try-catch statements inside if-else loops. This piece of code in Python won't be easy to read and solution isn't elegant and better than Perl either. Perl turns out to be better in all ways, the code is readable, concise, faster and powerful.
- jergosh 15y agoYeah, it's through eval() which is a questionable design decision at best. Regular expressions can be passed as objects in Python, too. Three functions is hardly 'innumerable'. "Perl turns out to be better in all ways, the code is readable, concise, faster and powerful." To each his own, I guess.
- kamaal 15y agoI don't think you got my point about regular expressions in Python. If its only one regular expression, then its 3 functions. But if say you have 20 regular expressions, now you have 60 function calls. Say you have to add 6 statements of exception handling to each of this, you now have 360 statements. Imagine this inside a giant if-else loop structure. Without a doubt the Perl solution is a lot more cleaner than Python solution.
- jergosh 15y agoIf you want to match a regexp in Python, you can just say: m = re.match("some_regexp", my_str) You don't need to compile regexps before use but you get better performance if you do when matching the same thing multiple times (no idea if there's a Perl equivalent). Also match() and search() have different semantics and you don't use them at the same time. So it's one statement in Python per match. Also how do you propose to get an equivalent of (non-mandatory) exception handling in Perl code without extra statements?
- ajross 15y agoPerl's regexes are part of the syntax of the language. They're compiled when the script is. That's why the perl syntax is cleaner and the implementation more robust. A bug table of regexes to parse fields out of a record or whatnot looks natural (i.e. declarative) in perl; in python it's kind of a big, imperative mess. Perl exception handling is quirky, but pretty clean. You raise an exception with die() and "try" a block with a simple eval. It's basically the same amount of syntax. The one bit perl lacks as a builtin is the type-based exception matching; you have to do that logic yourself.
- chromatic 15y ago[Perl 5 exception handling works] through eval() which is a questionable design decision at best. eval() and eval {} are very different constructs in Perl 5. The questionable design decision there is reusing the keyword.
- tene 15y ago
- lawn 15y agoI would really suggest you actually learn and use Perl (all of it) before trying to do such a hard task. Too often people just compare a language to their favorite without bothering enough with the other language.
- jergosh 15y agoI know it's a peril, you tend to know the thing you like best. I'm trying to address it by asking a guy who teaches the Perl course at my uni and other fanboys ;)
- sapphirecat 15y agoWhen I think "string handling" in a Perl-vs-Python sense, I think "regex syntax". There are 3 distinct tiers: Perl and Ruby's built-in syntax, PHP's preg_* functions, and finally the Python (and perhaps Java?) Extreme OO style. (In descending order by my preference.) Python's approach also eliminates the pattern of `if (preg_match(...)) { use captured groups; }` since the match object comes back as a return value instead of either a reference or magic variables, and assignment of that value is illegal inside the if's condition. Very Pythonic, but adds an extra line of code to assign the match separately from testing the result. (Edited a bunch because I fail at non-markdown.)
- tene 15y agoWhen working with regular expressions in Perl, the RegularExpressions::ProhibitCaptureWithoutTest perlcritic policy (default severity 3) helps avoid some common mistakes. http://search.cpan.org/~elliotjs/Perl-Critic-1.115/lib/Perl/Critic/Policy/RegularExpressions/ProhibitCaptureWithoutTest.pm http://search.cpan.org/~elliotjs/Perl-Critic-1.115/lib/Perl/...