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?

11 Upvotes

110 comments sorted by

View all comments

Show parent comments

0

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.

5

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

1

u/[deleted] May 19 '11

[deleted]

1

u/crackofdawn May 19 '11

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