Perl or Python, better suited for Unix system automation?

10,508

Solution 1

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.

Solution 2

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!

Solution 3

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.
Share:
10,508

Related videos on Youtube

nitins
Author by

nitins

I am Nitin :)

Updated on September 17, 2022

Comments

  • nitins
    nitins over 1 year

    I have been using bash for most of my system administration tasks. I also know a bit of perl.

    Should I learn Python or Perl is better for system automation. So far from my experience learning perl has been easy.

    • John Gardeniers
      John Gardeniers about 13 years
      Should I buy a black server or a white one? They both do the job, so pick the one that best suits you.
    • Joe Internet
      Joe Internet about 13 years
      You might be interested in reading "Minimal Perl". It shows how to use Perl as a replacement for many common shell utilities, and is targeted toward *nix users: minimalperl.com
  • nitins
    nitins about 13 years
    Yes I have heard Python is good coding in OO. But for sysadmins is writing in OO required. I don't think I will be writing anything above 100 lines.
  • Cakemox
    Cakemox about 13 years
    It's not required, no. You will encounter it, though, so it helps to be familiar with it.
  • Dasch33
    Dasch33 about 13 years
    I favour func (fedorahosted.org/func) for these sort of tasks - it to is written in python and does not rely on ssh.
  • pablo
    pablo about 13 years
    nice one, shame it only supports fedora/EL
  • Niall Donegan
    Niall Donegan about 13 years
    +1 I most definitely agree with "Worse even is when you encounter your own Perl after many years. You'll wonder at which point you must have blacked out." And you can replace "many years" with 6 months!
  • Steven
    Steven about 13 years
    As a sysadmin I regularly write over 100 lines. Usually once I hit 100 lines I start wishing I hadn't used bash :-) I'll use python (used to use perl) if I know if will be that long ahead of time.
  • Jeff Albert
    Jeff Albert about 13 years
    I know this question's way past closed, but it pains me to see the 'perl is just line noise' thing still making the rounds. Shell scripting is line noise too, if you do it badly. Python is no less susceptible to the 'big ball of mud' design pattern than any other high-level language. Just Say No to implementation zealotry; focus on reliable, maintainable patterns in whatever language fits your environment.