7

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.

nitins
  • 2,527
  • 15
  • 42
  • 65
  • Should I buy a black server or a white one? They both do the job, so pick the one that best suits **you**. – John Gardeniers Mar 09 '11 at 10:20
  • 1
    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: http://minimalperl.com/ – Joe Internet Mar 09 '11 at 11:38

4 Answers4

10

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.

Cakemox
  • 24,141
  • 6
  • 41
  • 67
  • 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. – nitins Mar 09 '11 at 09:31
  • 3
    It's not required, no. You will encounter it, though, so it helps to be familiar with it. – Cakemox Mar 09 '11 at 09:39
  • 1
    +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! – Niall Donegan Mar 09 '11 at 11:42
  • 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. – Steven Mar 09 '11 at 18:50
  • 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. – Jeff Albert Mar 14 '11 at 18:50
3

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!

Jeff Albert
  • 1,967
  • 9
  • 14
1

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.
pablo
  • 3,020
  • 1
  • 18
  • 23
0

They're equivalent; for a home machine, which you use depends on which one you feel more comfortable in, but when working with others you should adhere to any established standards (if everyone else uses Python you shouldn't use Perl without good reason, and vice versa).

geekosaur
  • 7,025
  • 1
  • 19
  • 19