Tag Archives: flame war

Why Perl is better than Python

Many people around me go on about how Python is better and easier to read and write than Perl.  I gave it a try with using Invent with Python. The 2nd piece of code made python crash due to a lack of backwards compatibility. Okay, every language does this, every now and then, but the code here was so small that crashing the shell seems as though there was an error.

print('Guess')
input()
import random

guess = 0

print ('What is your name?')
myName = input()

number = random.randint(1, 20)

print ('Well, '+ myName + ', I am thinking of a number between 1 and 20.')

while guess < 5:
    print ('Take a guess.')
    G = int(input())

    guess += 1

    if G < number:
        print ('Too low')
    if G > number:
        print ('Too high')
    if G == number:
        break

if G == number:
    guess = str(guess)
    print ('woo, you found it in '+guess+' turns.')

if G != number:
    number = str(number)
    print ('nope, twas ' + number)

Admittedly, I have had the most experience with Perl and its data structures.  However, experience is not an argument when it comes to comparing programming languages. The main argument I get thrown in favor of Python, is its readability. This in itself separates into the Python BLOCK syntax, as well as its general syntax.  Python doesn’t use curly braces {} to separate blocks, but rather indentation, often 8 spaces although certain schools recommend 4.  Moreover, Python has no instruction separator, other than EOL. Sure, on a script running many hundreds of lines, you’ll be happy the interpreter/compiler doesn’t snap back at you because of a missing “;”. However, you can also create a macro in Vim or Emacs or whichever editor of your choice to add a semicolon if an EOL is encountered outside of certain conditions (single/double quotes, heredocs, etc.).

The second argument of readability, is due to Perl’s capacity of inserting functions within functions.  However, when writing code the second thing to be thinking of is future you debugging said code to figure out WTH line 357 means. If line 357 is within a block, Shouldn’t there be some instruction as to what said block does? The same goes for subroutines and modules. Clutzy coding will ALWAYS lead to headaches. And while you might be writing code for a company where you’re not sure to be kept in order to maintain it, mixing reg-ex with functions can make it difficult if not impossible to understand from day to day.

These are the arguments I received from other programmers, and I feel seem almost religious statements from people who have had little to no hands-on with Perl (not that I have hundreds of hours with either, but Python has caused me more headaches). Why I love Perl:
Intuitive, getting a variable to auto-increment is impossible in python with a ++ operator. For someone coming from a basic C training, the ++ is the easiest way to work with loops.
Functional/Procedural based: Here both languages (sort of) even out. While Python is OO based, it isn’t as cluttered as say Java. However, both languages can be used with a fair amount of ease to create simple scripts. However, Perl has the advantage when it comes to complex functions within functions.  All it takes is one comment, and rather than append methods to the methods of objects, Perl allows for the funk-ception to be readable and fast.
Strict/warnings/diagnostics: When i started with Perl, these were the first lines of code I was told had to be everywhere right after the shebang. Typo’s are everywhere. If you use gcc, it will usually act up with bad variable names. Python may crash as well, but Perl, Perl tells you “hey buddy, you made a mistake on line xy, why don’t you go fix that while I take a nap.” Lazy? No. Everyone makes typos, be it a missing $, a trailing symbol before the EOL, so many reasons to have your interpreter help you out.  Exists in Python? You should be able to create a library for that, but then again, in Perl strict checks not only for declaration, but also for declaration within scope => cleaner programming.

It obviously comes down to personal preference, but IMHO, Perl has the upper hand when it comes to the learning curve, the languages capacity of enforcing good practices (well… I guess indentation might win Python a point there), and general community awesomeness.