jump to navigation

Perl Really Sucks (And They Don’t Even Realize It!) September 18, 2008

Posted by PythonGuy in perl, Python.
trackback

Perladmin takes the bait: Why “Perl Sucks” examples are always so funny is a response to my post Python Rocks (Perl Sucks).

He pretends like I’ve never heard of splice. Of course I’ve heard of splice. I’ve heard of all the functions, because I’ve read the perl man pages and reference books multiple times. It’s my job. I know perl better than anyone I know. And I work at a pretty big company with lots of very smart perl people. (I met Larry Wall once, but I can’t say that I know him, so he doesn’t count.)

But he misses the point.

Well, he got the point that I am obviously biased, in the same way a boy falls madly in love with a girl. But it isn’t blind love. I know python and perl well, too well, and I choose python.

First, let me answer a question he had:

In the Python example the variable group is an array data type so it doesn’t need de-referencing because its not a pointer. (Ahem!) Perhaps internally it is, (Good guess) and Python only has references and all functions know how to work with them. Good news for Python if it is. (Yep! Yep! Yep!) But if you’re using arrays in your Python script, you have to make an apples-to-apples (I just did.) comparison with Perl, in which case your variable is @group, and the variable doesn’t need de-referencing, thus we also resolve this issue.

The closest thing in perl to a python variable is the scalar. In fact, it is safe to say that in Python, all variables are the same thing as refs in perl. So, $group in perl is group in Python, provided that $group is a reference. (To what? It doesn’t matter.)

Now, he took several pages to try and justify perl’s existence. I will smash him with one simple point:

HE TOOK SEVERAL PAGES TO TRY AND JUSTIFY PERL’S EXISTENCE.

And in the end, this is the code he came up with, writting in perl and then python. Note that he forgets that $group and $maxNumInSubgroup may be expressions, not actual arrays. If that were the case, then you’d have to assign the expressions into $group and $maxNumInSubgroup rather than express it naturally in context.

Oh, and the #$group crap? Yeah, that refers to the @group, not $group. Got it? Don’t worry, it’s an interview question. If you see code like this in perl, you rewrite it the way I wrote it in my article so that mere mortals can understand it.

# perl
my @group = @{$group}; # Dereference, then array copy
my @subgroup = @group[0..(#$group > $maxNumInSubgroup ? $maxNumInSubgroup : #$group)];

# Python (equivalent)
group2 = group1[:] # dereference (no can do in Python!) then array copy
subgroup = group[:len(group2) > maxNumInSubgroup ? maxNumInSubgroup : len(group2)]

# Python (natural)
subgroup = group[:maxNumInSubgroup]

And he dares say:

The above end-result for the Perl code is slightly longer, but no less readable.

I pause now to allow you to clean your keyboards, catch your breath, lower your heart rate after the good belly laugh you enjoyed. Or not. Because he’s completely serious. He actually believes what he wrote, folks.

In the end, he discovered that perl’s splice and Python’s slice are two different things. See, perl’s splice changes the array being spliced. And Python’s slice, makes a new array copied from the old one. That’s a fundamental design decision.

But he mentions speed and performance. See, when you do a copy & splice in perl, you are wasting valuable time and memory. Part of that time is spent trying to remember which arguments to splice do what, and the other part is spent remembering that splice doesn’t do slice. The rest of the wasted time is spent typing it up and trying to remember that dereference @ binds more tightly than ->, so you have to put {} around the thing.

Oh yeah, and you waste some CPU and RAM as well, but who cares about that anymore?

Question: Which use case is more common?
Answer: Perl copy & splice, which is Python slice.

Question: Does Python even have splice capability?
Answer: Of course. del a[i:j] and a[i:j] = [...].

Question: Why doesn’t perl have a slice function? I mean, it’s a pretty common use case, and HOW HARD CAN IT BE TO ADD THE THING?
Answer: Stop shouting!

Other interesting things…

In fairness I should point out that the above examples would generate warnings under strict type checking. I have no idea if Python has an equivalent. If it doesn’t I would think that it is a hinderence to Python developers.

N.B what Python does vs. perl. You actually have to run this code to see why Python doesn’t need “use strict”. If you don’t run the code, you won’t understand what it does unless you already know Python and perl.

# A: perl
use strict;
print $foo;

# A: python
print foo

# B: perl
use strict;
my $foo = "bar";
print $foo;

# B: Python
foo = "bar"
print foo

If that’s too hard for you perl-gods to comprehend, let me interpret. Python’s assign is a declare and assign. That is, the “my” is hidden somewhere, obscured, forever banished. Python scoping rules are quite different than perl’s, eliminating the dynamic scope (aka, what “local” gives you) and relying only on lexical and calling scope (aka, what “my” gives you.) So we don’t need “my”, just like we don’t need @, %, $, or &, *, or anything else like that since there is only one kind of variable.

Now, as a serious aside, let me be bold in admitting that Python isn’t perfect. But it’s a world apart from perl. Heck, even Ruby kicks perl’s butt up and down the street, and Ruby sucks pretty bad too, in very serious and bad ways. When it gets right down to it, Python has some serious issues as well, but I consider them warts and not life-threatening cancers.

Perl was great, groundbreaking, revolutionary stuff in its time. My hat’s off to Larry Wall and the perl community who took on the programming language industry and won. But the victory celebration is long over and we’ve moved on with our lives to new and better fields. So has the perl community, at least most of it.

Comments»

1. Colin - September 18, 2008

I think you sort of missed my point. My point was not that Perl or Python was better, but that invariably the test cases are not apples to apples comparisons.

Your follow up post makes it very apparent that you believe I’m attacking you, or Python – which I’m not – and makes some extraordinary claims. I certainly had no idea from the convoluted examples that you’re so intimately familiar with Perl, but I’m more than happy to accept that. Neither was I questioning your devotion to Python as it seems very apparent.

Quite how my post is a “justification of Perl’s existence” I’m not sure, but then I’m not really into the “X is better” mudslinging contests being firmly in the “the right tool/person/job” camp. If I was such a Perl fanboi, do you think I would be using WordPress for my blog since it is written in PHP?

I did find the information you provided about how Python treats variables and arrays informative though as I have no experience with it. (which is another reason why I wasn’t claiming that Perl is better, I don’t know Python so I would probably end up with convoluted examples which compare badly)

Liked the last part, though perhaps the victory celebration should have had more fireworks, cus we didn’t hear it over here :-)

Nice site, keep up the good work.

2. Jonathan Gardner - September 19, 2008

As far as the victory celebration, the very fact that people talk about PHP, Python, Ruby, and perl when the market for languages is crowded by .Net, C#, and Java is a testament to Larry Wall and the others around him (as well as the language designers for the respective languages.)

The idea that a bunch of hackers could throw together a language that would put other languages to shame was preposterous when it was first proposed, but acceptable nowadays.

If you haven’t learned Python yet, I suggest you do. Maybe before you learn lisp and scheme, or after, or while. I think Python has a lot to teach any programmer, even old crusty ones.

3. draegtun - October 8, 2008

Pythonguy said: Question: Why doesn’t perl have a slice function? I mean, it’s a pretty common use case, and HOW HARD CAN IT BE TO ADD THE THING?

You’ve used it in your example… my $splice = @array[ $start .. $end ]; The only difference is that Perl does an array slice compared to Python’s list slice (which of course u already know about hence your original code gymnastics in Perl).

Pythonguy said: Python doesn’t need “use strict”. If you don’t run the code, you won’t understand what it does unless you already know Python and perl.

Python needs “use strict” probably less than Perl but it does need it! Try this “contrived” example….

loop = 0

while loop < 5: loopy = loop + 1 print loop print "done %s", loop [/sourcecode] And it will loop indefinitely ;-( The same would happen to Perl..... [sourcecode language='ruby'] use strict; use warnings; my $loop = 0; while ( $loop < 5 ) { $loopy++; print $loop, "\n"; } print "done $loop"; [/sourcecode] ... if it wasn't for "use strict" spotting the mistake with $loopy ;-P I've use pychecker & pylint before but these are both external source code checkers and so break the development flow unlike "use strict" (and don't forget "use warnings") when developing in Perl. Pythonguy said: Perl was great, groundbreaking, revolutionary stuff in its time. My hat’s off to Larry Wall and the perl community who took on the programming language industry and won. But the victory celebration is long over and we’ve moved on with our lives to new and better fields. So has the perl community, at least most of it.

Well we maybe living in different echo chambers (and probably different universes!) but your last bit is totally incorrect and smells of FUD.

/I3az/

Andrew Stormont - December 7, 2011

Python doesn’t need ‘use strict’. Infinite loops generate an exception during runtime:

RuntimeError: maximum recursion depth exceeded

PythonGuy - December 7, 2011

I think you mean “infinite recursion.”

Of course, every language will have the same problem with infinite recursion, unless they use tail recursion and the algorithm allows it. No one has infinite memory on their system.

4. Hitesh Kumar - December 10, 2011

What exactly is “[:insertcodehere]” in your Python example? The [:, I mean. What is that splitting?

5. JensE - June 4, 2013

If you misspell a variable somewhere — and this IS possible without creating an infinit loop — it’s hard to find in python.

Joseph - November 5, 2014

Not if you’re using tools like PyLint or essentially any IDE (which will be using PyLint or similar under the hood).

If you make a mistake on the variable side, e.g. loopy = 7 instead of “loop”, you’ll get a “variable declared but never referenced” message. Similarly if you slip up on the value side, e.g. x = loopy + 1 you’ll get a message that “variable is referenced but never initialized”.

6. superman - April 7, 2014

Your

 html element seems to be formatted poorly, as the text goes over the edge of the box but there's no scroll bar, so I have to copy-paste everything in the pre box to find out what it says. If you put pre {overflow:scroll} in your css, it should work. I've tested it out on this page and it does the trick. 

Thanks
superman - April 7, 2014

didn’t realize it would reformat my post when i wrote “” above, here’s what I said:

Your “pre” html element seems to be formatted poorly, as the text goes over the edge of the box but there’s no scroll bar, so I have to copy-paste everything in the pre box to find out what it says. If you put pre {overflow:scroll} in your css, it should work. I’ve tested it out on this page and it does the trick.

Thanks

7. Darren Meyer - April 15, 2015

I don’t quite understand why you felt the need to dereference and array-copy as the first step in your Perl example; seems like an unnecessary extra step. Anyhow, as someone who uses both Perl and Python regularly, and likes them both for different purposes:

 
# Python (natural)
subgroup = group[:maxNumInSubgroup]

Vs.

# Perl (perlish)
@subgroup = $group[0..min ($#group, $maxNumInSubgroup)]

# Perl with $group as an array reference, if you insist:
@subgroup = $group->[0..min ($#{group}, $maxNumInSubgroup)]

Perl is a bit more verbose here, to be sure, but it reflects only a difference in how Python and Perl language design think about what a “list” is conceptually. Neither approach is better or worse objectively, it’s simply that Python’s design more closely matches how you conceptualize the problem. Which is great! That’s why we have many different languages.

All languages suck to some extent, as they’re imperfect representations and so reflect the flaws of the humans who design them; when you say one “sucks”, you’re saying that people who have a different way of thinking about problems are incorrect as opposed to different.

Darren Meyer - April 15, 2015

In my haste, I used improper syntax:

@subgroup = @group[0..min ($#group, $masNumInSubgroup)];

or

@subgroup = @{ $group }[0..min ($#group, $masNumInSubgroup)];
nomel (@nomel) - February 26, 2016

Your need for an edit kinda proves the whole point.

boopity - June 9, 2016

@nomel Of course! Python programmers never make mistakes. But doesn’t that mean mere mortals should stick to other, less perfect, languages such as Perl? I’m confused.

8. jumpy - April 20, 2020

I’m not a programmer but I hate how some programming languages try to be fancy with naming conventions. I can take a wild guess at what a function, type, pointer, string, integer or even an array is. But stuff like scalar, goroutines or the python method of declaration. Just sounds bogus concepts to explain logic.


Leave a comment