jump to navigation

Python Rocks (Perl Sucks) March 4, 2008

Posted by PythonGuy in perl, Python.
trackback

Arg! Reading this in perl will make you scream, even if you know and love perl:

my @subgroup = (scalar(@{$group}) > $maxNumInSubgroup) ?
    @{$group}[0..($maxNumInSubgroup-1)] :
    @{$group};

First, I hate the way perl doesn’t treat every variable as equals. Why can’t everything just be a ref like in Python? Because of this, you have to worry about dereferencing, which makes nobody’s code beautiful. Even when you try to be consistent (as the author above is, thankfully), you still have to know about the other five or six methods of dereferencing.

Aside from that, Python’s indexing is one of its biggest strengths because it is consistent and beautiful. The above translated into Python:

subgroup = group[:maxNumInSubgroup]

Note that the resulting subgroup will not exceed the size of group, even if group has less items than maxNumInSubgroup.

Note that you have no “-1″‘s to get the array the right size.

Note also the absence of casts. Perl’s scalar cast on a list is one of the most wrong-headed decisions of all time. If I wanted to know the length of an array, I would ask for it explicitly. Of course, I hardly ever care what the length of an array is.

One more benefit… count the number of times you had to write “group” and “maxNumInSubgroup” in the perl and Python examples. Since you only had to write it once in Python, you can substitute the variable name for the expression that gave you the value in the first place. For example, compare the following two code samples:

Perl:

my $maxNumInSubgroup = pageSize/3 + 5;
my $group = getGroup(param1, param2);
my @subgroup = (scalar(@{$group}) > $maxNumInSubgroup) ?
    @{$group}[0..($maxNumInSubgroup-1)] :
    @{$group};

Python:

subgroup = getGroup(param1, param2)[:pageSize/3+5]

Not having to be repetitive means you don’t have to come up with new temporary variable names when they aren’t really necessary. It also reduces the complexity and length of your code.

Python wins. Perl loses.

Comments»

1. BKB - March 5, 2008

Your Perl programming skills here are what lose. Firstly,

scalar(@{$group})

is just a crazy way of writing @$group (which does exactly the same thing here). Second, the effect you’ve achieved is much simpler to get like this:

my @sgroup = @$group;
splice @sgroup, $maxNumInSubgroup;

which similarly does exactly the same thing as the rather long expression you’ve typed in.

The moral of today’s story is: Perl: don’t knock it if you don’t know it.

2. PythonGuy - March 5, 2008

Ummm…. no.

Now, I admit that you can make the above perl code more beautiful. However, the above is real perl code I found in the wild by people who write perl code all day for a living. Go ahead and knock them, but this is what perl gets you. I could spend all my time rewriting people’s perl code to “proper” perl, but then I get complaints because every time I touch a file, I have to rewrite the whole thing.

Also, your little bit of code doesn’t do exactly what the perl code and python code does. See, you have an extra array copy. See if you can find it yourself before you get knocked by some other perl expert.

And I would really like to find someone mangle the Python sample I gave. Can’t do it, because Python makes it really hard to write dumb Python code.

3. BKB - March 6, 2008

I saw an interesting post about lengthy Python today and remembered our discussion:

http://www.ginstrom.com/scribbles/2008/02/14/intermediate-python-pythonic-file-searches/

4. Why “Perl Sucks” examples are always so funny. | Perladmin - September 17, 2008

[…] post is so funny. The post in question is the (obviously unbiased) opinion piece entitled Python Rocks (Perl Sucks) on a Python dev’s web […]

5. Perl Really Sucks (And They Don’t Even Realize It!) « Python Guy - September 18, 2008

[…] Perl Really Sucks (And They Don’t Even Realize It!) September 18, 2008 Posted by PythonGuy in Python, perl. trackback Perladmin takes the bait: Why “Perl Sucks” examples are always so funny is a response to my post Python Rocks (Perl Sucks). […]

6. draegtun - October 5, 2008

re: list/array slice – I agree that in most cases just returning the “truncated list” and not the “specified array” is very handy indeed. However I do have one project where getting back the array specified is vitally important (and I suspect a lot of mathematicans would also agree!).

So as long has u can do both then your programming languages doesn’t suck ;-)

re: your Perl examples – Thankfully we have TIMTOWTDI….

my @subgroup = @$group[ 0 .. min( $#{ $group }, $maxNumInSubgroup - 1 ) ];

my @subgroup = splice @{ getGroup($param1, $param2) }, 0, $pageSize/3+5;

/I3az/

7. PythonGuy - October 7, 2008

TMTOWTDI really sucks when there’s no good way. Having lots of bad ways to do something doesn’t make a good language.

Your first example is hard to read. The $# operator is rare and almost looks like it should be a comment. In fact, the syntax highlighter in your comment believes it to be a comment. You also have to add in a -1 or suffer from an off-by-one error. If you go through your code, you’ll find that the idiom you’re using is so common that it should be built into the language itself. In fact, I can’t think of any reason you’d ever want to do anything else.

And the syntax for array slices of arrayrefs: Tell me, if $group is an expression, how do you write that? Do curly braces go around $group or @$group, or do you need an arrow operator in there somewhere? If you have to spend more than five seconds thinking about that, your language fails.

Your second example has a big bug. If you can’t see it you shouldn’t be using perl. (Hint: What happens when getGroup doesn’t return an array copy?)

You lose, and you don’t even realize it.

8. draegtun - October 8, 2008

re: TIMTOWTDI….

You’re absolutely right… and to correlate this having only one way of doing something doesn’t make it a good language either (if it sucks then u’re really fscked!).

I do like Python’s tighter syntax (though I really hate hanging blocks & vertical alignment issues but hey thats just me and its only cosmetics). I think Ruby syntax is even nicer but its VM & libraries aren’t mature enough yet (its where Perl was circa 1995-2000 & Python circa 1999-2005).

So don’t get too hung up on syntax and forget about expressiveness. Syntax alone doesn’t make any language “superior” to any other. Here’s a nice analogy which I’ve just thought of (so take it with a pinch of salt!)….

Python is like German… grammatically correct language.
Ruby is like Italian…. beautiful, elegant and graceful.
Perl is like English…. a complete bastard of many languages & dialects ;-(

However some how Shakespeare was written in English ;-)

re: $#

Yeah your language must really suck if the WordPress syntax highlighter doesn’t highlight it correctly… LOL

If you want it more DRYer then simply abstract it away in a subroutine….


sub slice (@) {
my ( $array, $end ) = @_;
return @$array[ 0 .. min( $#{ $array }, $end - 1 ) ];
}

my @subgroup = slice $group, $maxNumInSubgroup;

NB. Python made the decision to put a lot more into the core. Perl instead went all “organic” on us and chose the CPAN ecosystem. There are lots of CPAN that is really considered as core (and may well be at future points). So if extending language via subroutines is a pain then there’s plenty of modules on CPAN to ease that suffering.

BTW… $# isn’t rare at all.

re: -1

Actually its Python that gets a little inconsistent here…

limit = 10
array = range(0,limit+1)    # yikes! 
print array[0]         # 0
print array[1]         # 1
print array[2]         # 2
print array[:2]        # 0,1   
print array[0:2]       # 0,1   
print array[1:2]       # 1     

Perl is consistent and sticks to array positions….

my $limit = 10;
my @array = 0..$limit;
say $array[0];             # 0
say $array[1];             # 1
say $array[2];             # 2
say @array[ 0 .. 1 ];      # 0,1
say @array[ 0 .. 2 ];      # 0,1,2
say @array[ 1 .. 2 ];      # 1,2

So there will be cases where Perl will need “-1” there will be other cases where Python will need “+1”.

And with splice things can look a bit odd in Python….


array[2:4] = ['C', 'D']

splice @array, 2, 2, qw/C D/;

Perl example is more intuitive here than the Python one. This then leads to things to Python having to do some gymnastics….


y = ['c', 'd']
array[2 : len(y) + 2 ] = y

my @y = qw/c d/;
splice @array, 2, @y, @y;

You see computer language perfection will always remain an enigma as long as we’re still part of the equation ;-)

Pythonguys said: “And the syntax for array slices of arrayrefs: Tell me, if $group is an expression, how do you write that? Do curly braces go around $group or @$group, or do you need an arrow operator in there somewhere? If you have to spend more than five seconds thinking about that, your language fails.”

A bad workman blames his tools! If you have struggled for more than 5 secs to work out how to use $group as an expression then you can only blame yourself because 1) Either u haven’t learnt enough about the language or 2) You have bad memory! ;-)

“your language fails” is totally bogus. I’m no language bigot…. I use Perl more because i) I like it! ii) I’ve learnt enough to bend it to my requirements & iii) There’s more Perl jobs around (and a magnitude higher than Python or Ruby in my experiences here in UK. And I’d guess its the same over in the US).

Anyway it took me less than 5 seconds so “the language didn’t fail me!”. NB. Strangely the only time “a language has failed” me was a Python project some years ago. I took over a prototype program written in Python and spent about 4 weeks improving it in a tight phase 1 deadline for a million £ project. After phase 1 deadline passed it was apparent it needed rewriting for next phase so I re-did everything in Perl in about 2 weeks. However I don’t think Python “failed me” rather I was better skilled to do it in Perl… but looking back at it I wish I decided to rewrite in Perl from day one rather than trying to push the Python prototype version.

Pythonguy said: “Your second example has a big bug. If you can’t see it you shouldn’t be using perl. (Hint: What happens when getGroup doesn’t return an array copy?)”

Thats a Schroeder’s cat assertion you’re making there ;-)

The second example has no “big bug” because there is no “getGroup()” defined in the example to produce any bugs!

Excluding bad design there is no need to make any artificial copy of an array before returning the reference (Hint: Read up on what happens when you return references to lexical variables from a subroutine).

Pythonguy said: “You lose, and you don’t even realize it.”

What an interesting concept? ;-)

On that note I bid you goodbye… “So long & thanks for all the fish”. Perhaps you will find yourself better relief by getting a Python job instead of hacking away at Perl each day? Good luck!

/I3az/

9. lucky - November 26, 2008

well people here are trying to prove Perl bad using illogical things, they r comparing using Perl core references example and simple : ? statement of python which is also present in the Perl. so from the my point of view Perl is a lovely language and these baseless critics are likely to gain nothing…. :) cheers for Perl.

10. nobody - December 26, 2010

This post is biased.

Both Perl and Python are fantastic languages. The problem is that a lot of new programmers try Perl and start with bad programming practices and as they don’t get rid of these habits when they learn the language further. The strength of Perl is in its ability to accomplish a task using several ways.

Personally, I hate the indentation only type blocks in Python. Although I’ll admit, that I like how all the python variables are like refs in Perl.

PythonGuy - January 5, 2011

In real languages, being able to express an idea in multiple ways can make the language incredibly difficult to use. I’d rather have one correct way to do it than a few correct and a lot of incorrect ways.

PythonGuy - January 5, 2011

BTW, of course it is biased. I evaluated perl and Python and decided Python is dramatically superior.

11. Five things to like about Perl « things i learned today - May 18, 2012

[…] like programming. If that’s the case, you were probably raised to believe that Perl is evil. I certainly was. I heard all of the lousy things about how it’s totally unreadable, and how […]


Leave a reply to nobody Cancel reply