Perl or Python, better suited for Unix system automation?

Short answer: learn both.

You are going to encounter both as a sysadmin, so you'll want to know how to read/troubleshoot/debug both.

As for writing scripts, I have used mostly Perl over the last 10 years for most of my sysadmin utility and "glue" scripts. Its regex syntax is really simple, and it lends itself quite well to extremely fast script development. This is quite important when you have to get something working on-the-spot.

Lately I have been making an effort to use more Python for the following reasons:

  • It takes a lot of discipline to write good Perl. All too often a "quick n' dirty" script has more features crept in over time, yet still implemented in a hasty manner. Before too long, you have a sprawling file of line noise which is a PITA to maintain.

  • Writing OO (or even reusable) code in Perl is not easy compared to Python. Believe me, in the long run you want lots of reusable code (and not cut-and-paste!)

  • Python's principles (import this) are much better suited to collaborating with others when compared to Perl's TMTOWTDI principle. If you've ever read someone else's Perl, you may know that it can be about the most frustrating thing to unravel. Python suffers from this unreadability problem far less due to its design. Worse even is when you encounter your own Perl after many years. You'll wonder at which point you must have blacked out.

  • Documenting is important if your code is going to be around a while. Writing docstrings in Python is much easier than writing pod markup in Perl. Easy enough that you might actually use it.

I still use Perl quite a lot, but it's now more for one-liners and "throwaway" scripts which will only run once. If I think I'm ever going to edit the script again, I consider Python instead.


It depends on what you're trying to do, and where you're trying to do it. All things being equal, and where you have no restrictions on your environment, can install whatever you want, and don't have to worry about interoperating with legacy code, feel free to pick the language that suits your personal preferences best.

That said, for sysadmin work, I do think Perl has an edge: it's installed on everything out of the box, and has been since roughly the dawn of time. If you're writing a system automation or management script and using only core Perl modules, you can be almost positive that it will run without modification everywhere in your heterogeneous UNIX environment, and can be extended fairly painlessly to Windows with an installation of ActiveState or Strawberry Perl.

Hope that helps!


I would recommend python. I know perl enough but am not an expert and my experience with python has been vastly superior. I think it depends on your needs but if its simply for system automation I would go the python route and forget about perl (for the time being).

Here is a great way to sink your teeth in:

http://diveintopython.org/toc/index.html

With things like Fabric in development it can really make things easier:

http://docs.fabfile.org/en/1.0.0/index.html

Example from the page:

from fabric.api import run

def host_type():
    run('uname -s')

Output

$ fab -H localhost,linuxbox host_type
[localhost] run: uname -s
[localhost] out: Darwin
[linuxbox] run: uname -s
[linuxbox] out: Linux

Done.
Disconnecting from localhost... done.
Disconnecting from linuxbox... done.