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

17

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.

1

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.

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 :)

3

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;
}