5 ms·
I do love Perl. However, for data science, the biggest drawback vs Python is how you work with complex data structures. Because everything in Python is an obj
by tyingq 5y ago
I do love Perl.
However, for data science, the biggest drawback vs Python is how you work with complex data structures. Because everything in Python is an object, (de)referencing things is generally straightforward, like:
somelist[12]['whatever']="abc"
mylen=len(somelist)
Where Perl devolves into a mess of sigils, when to use $ vs % vs @, wrapping references with {}, etc.
$somelist[12]{'whatever'}="abc";
$mylen=length(@somelist);
This gets more complex as the nesting deepens, or as you add things like objects, want to iterate keys of a dict, and so on.
Edit: A real example from the perl data structures (perldsc) man page:
print "it turns out that $TV{$family}{lead} has ";
print scalar ( @{ $TV{$family}{kids} } ), " kids named ";
print join (", ", map { $_->{name} } @{ $TV{$family}{kids} } );
- d0mine 5y agoIt is not entirely about the syntax, for example, the code looks similar in Python (if I remember Perl correctly): print(f"it turns out that {TV[family]['lead']} has ", end=‘’) print(len(TV[family]['kids']), " kids named ", end='') print(*[kid.name for kid in TV[family]['kids']], sep=', ') For me, it was the culture “there is _one_ obvious (for a Dutch) way to do it” vs. “many” in Perl and most people lack discipline/good taste to handle the [too much] freedom provided by Perl. Somehow Python took the niche of “executable pseudocode”, “glue language” for me.
- tyingq 5y ago>the code looks similar in Python Minus the sigils and crazy wrapping, like: @{ $TV{$family}{kids} } to refer to the array, but a different sigil/form if you're talking about one element, etc. Which may not look intimidating on its own, but it's leaving out % and the backslash \$reference syntax, $object->method and other things that get confusing. You can't, for example really make a "list of hashes/dicts". Try it, and it just turns the keys/values into array elements. You have to specifically make a "list of references to hashes/dicts". So you do something like: push(@list,\%hash); Then, fun things like a double sigil: foreach my $hashref ( @list ) { print keys %$hashref; } Note there are several ways to do all this, and I am highlighting some of the more visually confusing ones.
- fragmede 5y agoFor printing strings, there is more than one way to do it in Python; differently readable for. print("""It turns out that {} has {} kids named {}""".format( TV[family]['lead'], len(TV[family]['kids'], ', '.join(*[kid.name for kid in TV[family]['kids']]*) ) It doesn't use the nifty f string formatter, but the English sentence part is a bit easier to read.
- powerapple 5y agoI used Perl for a lot data processing, mostly text. That time, our machine learning code was mostly C/C++. Perl slowly died before the big frameworks took off. It didn't compete with Python to be honest. Of course, even if there was a direct battle between Perl and Python, Python would probably win anyway.
- tyingq 5y agoI do find Perl much easier for manipulating text files, file names, and the sorts of things where bash or awk are too tedious. Then there's a line where something like Python becomes more attractive. Perl is still a lot faster than Python for many tasks too...the Perl devs seems to spend more time optimizing certain paths. Or perhaps "everything is an object" in Python slows everything down?
- nocman 5y agoSome people don't like sigils on variable names, and I get that. Personally I find them useful. As far as the perldsc page example, I think that comes down to having a better personal style that makes your code readable. I would make the top level variable a hash reference rather than a plain hash, and purposefully not insist on dereferencing multiple levels all on one line. As someone pointed out in another comment, you can make the Python version look just as lousy by virtue of a lousy style. My version of code doing the same task would be something like this: my $familyData = $TV->{$family}; my $lead = $familyData->{lead}; my $kidsList = $familyData->{kids}; my $kidCount = scalar( @{ $kidsList } ); my $kidListStr = join ( ", ", map { $_->{name} } @{ $kidsList } ); print "it turns out that $lead has "; print "$kidCount kids named $kidListStr\n";
- deleted 5y ago[deleted]