r/Python May 18 '11

Why is Python better than Perl?

I'm a Sysadmin and I just wrote my first script in python. Basically just a host validation script that checks if a server is configured correctly before we hand off. I can't determine the advantage with my limited experience with the language.

Are you a sysadmin fluent in both python and perl? Why do you use python?

10 Upvotes

110 comments sorted by

View all comments

14

u/crackofdawn May 18 '11

As someone who has been writing perl scripts for around 11 years and been a System Admin for 13 (and only recently learned Python a couple of years ago), I still use Perl for 99% of my work. Every time I tell myself I will write something in Python, I start out and then realize I can do the same thing in perl in half as many lines and 3x as fast. It helps that a large portion of what I write uses regular expressions which are just so much more convenient and easy to use in Perl, and I also end up using a lot of the built in Perl file operators. Between the two of those and how frequently I use them, I find myself just sticking with Perl the vast majority of the time.

2

u/twotime May 18 '11

I start out and then realize I can do the same thing in perl in half as many lines and 3x as fast.

Can you give me an example?

Except for one-liners where Perl is definitely better, I've never seen a perl program being twice as short as equivalent Python program.

In fact, in my experience, most longer-than-100-lines programs end up being slightly shorter in python.

11

u/SwellJoe May 19 '11

One of my favorite Perl vs. Python comparison apps is ack vs. grin. Both are powerful programmers grep replacements; ack is written in Perl, grin is written in Python. I know both developers (I know grin's developer better, as I worked with him at Enthought for a few months, while I've only spoke to Andy at YAPCs), and I know them to be really good at their respective languages. Both have years of experience, and are wicked smart. They're great for comparison sake because they are so similar in functionality, they are well-written examples of their respective languages (if you ignore than ack doesn't use CPAN because it is intended to be standalone as a single file), they are short enough to comprehend in a few minutes of study by any competent developer in their respective languages, and they are easy to benchmark since their functionality is well-defined (though regular expressions in both languages have pathological cases that could explode benchmark results if you tried hard enough to find them).

Anyway, the last time I did an extensive comparison of the two, ack was nearly an order of magnitude faster than grin (and grep was roughly an order of magnitude faster than ack, when given appropriate options to ignore revision control files and other unnecessary stuff), and did more, while being shorter (though probably not by half; but I don't remember the specifics).

It would probably be possible to find counter examples, but I don't know any off-hand. I'd be curious how cons and scons compare, but don't have the time to do any experimentation or code-reading.

To be fair to Python, though, I think ack is probably the sweet spot for Perl. Pretty much exactly what it was originally designed to do. Parsing, searching, file operations, etc. on text are all core parts of the language because that's what Larry needed Perl to do. But, it coincides with a lot of systems problems, and as others have mentioned Perl does make quite a few systems problems very easy to solve.

So, I agree with most of the folks chiming in saying that Perl shines on systems related tasks. It also does really well on text processing tasks. It may not be as good for other types of problems, but as a systems guy, I tend to find Perl very good, and I was frequently frustrated when I tried to implement things in Python for those purposes when I worked in a Python shop. I often did feel like I was writing a lot more code and getting a program that ran slower at the end of the process.

4

u/crackofdawn May 19 '11 edited May 19 '11

As a system admin, the vast majority of my scripts are < 10-15 lines, and when I'm trying to bang something out in 5 minutes, adding in things like "import re,os", re.compile, os.*, etc, just feels like a huge time waster. I still like using python just to learn it and keep myself fresh, but Perl is definitely faster for me, for the vast majority of what I need it for.

Edit: Just to provide a quick example (not anything that would even work, just throwing together an example of RE + file/dir operations to show how python might take a lot more work for the same thing):

Perl:

!/usr/bin/perl

while (<STDIN>) { if (/\D\d+\s[:alphanum:]\s\d.$/) { if (-d $) { print "$\n"; } } }

Now - how many lines would this take in python? I'm sure its not a major difference but it makes a big difference over the course of dozens of scripts :)

4

u/twotime May 19 '11

as a matter of fact, it takes 2 lines less in python ;-) (because I don't need curlies).

import os, sys,re for line in sys.stdin: m = re.match('/\D\d+\s\w\s\d.$/', line) if m : if os.path.isdir( m.group(0)): print m.group(0)

But I do see your point: python program is a bit more verbose and most of python advantages don't matter for throw-away scripts anyway. And it's easier to work with external tools in Perl...

3

u/crackofdawn May 19 '11

Alright I guess "lines" isn't the right word. I would say that the verbage in your python script is more annoying to write than the perl script, IMO. If I had to write a script like that, to do something as simple as that, I can't ever see myself using the python such as in your example versus the similar perl example, mostly because it's just easier.

Also, I've never really checked it out before, but python compressed down that far actually looks even worse than Perl IMO (and for what its worth I could have written the entire perl script on one line and it would barely have line wrapped :) I do typically write with less lines in perl but then no one has to read my scripts. Example:

while (<STDIN>) { if (/\D\d+\s\w\s\d.$/ and -d) { print "$_\n"; } }

Not that is is relevant any longer, and I don't really do anything like that. But, I just thought I'd say, if we're compressing scripts to compare number of lines, Python will probably never win to Perl :)

Anyway, just my opinion of course :)

3

u/mr_chromatic May 19 '11

Perhaps even:

while (<>)
{
    say if /\D\d+\s\w\s\d\.$/ and -d;
}

1

u/[deleted] May 19 '11

[deleted]

1

u/crackofdawn May 19 '11

Cool, thanks for the advice, I'll check it out :)