Dive Deep Into Python Vs Perl Debate – What Should I Learn Python or Perl?

Often when a new programming language is introduced, there is a debate that starts amongst some of the genius minds in the industry wherein the language is compared with the one already spreading its roots. A kind of buzz often spreads in the IT industry and the new one is often compared on every aspect may it then be features, syntax or core CPU and memory aspects including GC time and all, with the existing one of its kind.

Many examples of such cases can be picked up and investigated from the past including the debate between Java and C#, C++, etc. One such case which drew a significant amount of attention was the debate between two languages which came out one after the other in a short span i.e. Python and Perl.

Whereas Python was invented initially as a successor to ABC language merely as a “hobby” programming project (which would attract Unix/C hackers) for the author who named it after the series of his biggest star Monty Python.

Suggested Read: Getting Started with Python Programming and Scripting in Linux

Perl was just nearly around 2 years earlier as a Unix scripting language which intended to make report processing easier. It was a mixture of a combination of many languages including C, awk, sed, and shell script.

The thing which is worth noting is that these languages which evolved of different intentions are being constantly compared, which has made me study and figure out the reasons, of which some important ones are listed as below:

  1. Both targeted Unix Operating System, one for hackers and others to process reports.
  2. Both are object-oriented (Python being the more) and interpreted, with one being strongly typed and clear when it comes to coding i.e. Python, and other allowing ugly typing with braces for representing a block i.e. Perl
  3. Both are opposite in principle when we say, Perl has many ways of doing a single task while python focuses on one and only one way of doing things.

Python vs Perl – Features Compared

Let’s dive deep into this debate and try to figure out the overall aspects where these two languages differentiate from one another. Also, let’s try finding out the source of truth for many cliches which can be heard in the industry saying “Python is Perl with training wheels” or “Python is similar to Perl but different” so that we can try and conclude with an accurate solution to this never-ending debate.

1. Python’s Clean vs Perl’s Complex Syntax

Python takes a huge advantage over Perl when it comes to code readability. Python’s code is a lot clearer to understand than that of Perl even when reading code after years.

With indentation representing the block of code, and proper structuring, Python’s code is a lot cleaner. On the other hand, Perl borrows its syntax from various programming languages like C, shell scripting and even awk and sed filters when it comes to regular expressions.

Suggested Read: 15 Useful ‘sed’ Commands for Daily Linux System Administration Tasks

Apart from this, with ‘{‘ and ‘}’ representing a block of code and unnecessary addition of ‘;’ at the end of each line, code in Perl could become a problem to understand if you read it after months or years because of its allowance of ugly scripting.

2. Perl’s Built-in Vs Python’s 3rd Party Regex and OS Operations Support

Perl language borrows its syntax from C and other UNIX commands like sed, awk, etc. due to which it has way powerful and built-in regex support without importing any third-party modules.

Also, Perl can handle OS operations using built-in functions. On the other hand, Python has third-party libraries for both the operations i.e. re for regex and os, sys for os operations which need to be ensured before doing such operations.

Perl’s regex operations have ‘sed’ like syntax which makes it easy not only for search operations but also replace, substitute and other operations on a string can be done easily and swiftly than python where a person needs to know and remember the functions which cater to the need.

Example: Consider a program to search for a digit in the string in Perl and Python.

Python
Import re
str = ‘hello0909there’
result = re.findall(‘\d+’,str)
print result
Perl
$string =  ‘hello0909there’;
$string =~ m/(\d+)/;
print “$& \n”

You see the syntax for Perl is way easy and inspired by sed command which takes advantage over Python’s syntax which imports third party module ‘re’.

3. Python’s Advanced OO Programming vs Perl’s One-Liners

One feature where Python overshadows Perl is its advanced OO programming. Python has extensive object-oriented programming support with clean and consistent syntax while the object OOP in Perl being outdated where the package is used as a substitute for classes.

Suggested Read: Getting Started with Python Django Web Framework

Also, writing OO code in Perl will add a lot more complexity to the code, which would eventually make code difficult to understand, even subroutines in Perl are very difficult to program and eventually difficult to understand later.

On the other hand, Perl is best for its one-liners which can be used on the command line for performing the various tasks. Also, Perl code can eventually do various tasks in fewer lines of code than python.

A shortcode example of both languages which highlights Perl ability to do more in less LOC:

Python
try:
with open(“data.csv”) as f:
for line in f:
print line,
except Exception as e:
print "Can't open file - %s"%e
Perl
open(FILE,”%lt;inp.txt”) or die “Can’t open file”;
while(<FILE>) {
print “$_”; } 

Pros and Cons – Python vs Perl

In this section, we will discuss the Pros and Cons of Python and Perl.

Python PROS:

  1. It has a clean and elegant syntax which makes this language a great choice as the first programming language for novices who want to have hands-on on any programming language.
  2. Has very advanced and inherent OO Programming, also thread programming in Python is way better than Perl.
  3. There are many application areas where Python is preferred and even it outperforms Perl. Like: Perl is preferred for CGI scripting but nowadays Python’s Django and web2py like web scripting languages are becoming more popular and have a huge attraction from the industry.
  4. Has several SWIG wrappers for different programming languages like CPython, IronPython and Jython and development of these has preceded the development of SWIG wrappers for Perl.
  5. Python code is always well indented and easy to read and understand even if you are reading someone else’s code or even your code after years.
  6. Python is good for various applications like Big Data, Infra Automation, Machine Learning, NLP, etc, it has huge support of active communities because of being Open Source.

Python CONS:

  1. There are few areas where execution in Python is usually slower than that of Perl including regex and string-based operations.
  2. Sometimes it is difficult to get the type of variable in Python as in cases of very large code, you have to go till the end to get a type of variable that gets hectic and complex.

Perl PROS:

  1. Perl has powerful one-liners and even ensures UNIX piping like syntax which can be used on the command line to perform various tasks, also it is influenced by Unix and its command-line programming so integrates many UNIX influenced commands in its coding.
  2. Perl is known for its powerful regex and string comparison operations as it is influenced by sed and awk like powerful UNIX tools. In the case of regex and string operations like substitution, matching, replacement, Perl outperforms python which would take a few lines of code to achieve the same. Also many file I/O operations, exception handling is done faster on Perl.
  3. When it comes to a language for report generation, Perl has always been in fame since its introduction as one of the main reasons for the author to develop language like Perl was for report generation.
  4. Many application areas where Perl finds its use are Network Programming, System Administration, CGI Scripting (here Python is overcoming Perl with Django and web2py), etc.
  5. It is easy to identify the type of variable with the symbols that Perl uses before them, like: ‘@’ identifies arrays and ‘%’ identifies hashes.

Perl CONS:

  1. Perl has a very complex code which makes it difficult to understand for a novice. Subroutines, and even other symbols like: ‘$`’, ‘$&’ etc are hard to understand and program for a less experienced programmer. Also, Perl code when read would be difficult and complex to understand unless you have a quality experience.
  2. OO Programming in Perl is a bit out of date as it has never been known for OO programming and many operations like threading are also less pronounced on Perl.

Conclusion

As seen above where both languages are good on their regard as per the applications they target, Python takes a bit of advantage over Perl as a first choice for a novice due to its clean and easy to understand code, whereas on other hand Perl outperforms Python when it comes to string manipulation operations and some advanced one-liners for UNIX like OS and various other operations it is known for.

So, in the end, it’s all upon the specific area you target. All your comments on this article are welcome and would request you to give your views on the topic if according to you Python wins or Perl.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Gunjit Khera

Currently a Computer Science student and a geek when it comes to Operating System and its concepts. Have 1+ years of experience in Linux and currently doing a research on its internals along with developing applications for Linux on python and C.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

37 Responses

  1. Tim Van den Langenbergh says:

    The major breaking point for Python, why I can’t recommend it to anyone: UTF-8. It’s a lot to get into, but long story short: Unicode in Perl is tricky, in Python it’s fundamentally broken.

    Unless you’re only ever going to be working with people who speak ‘murrican, don’t use Python.

    Also: Perl 6 for the win.

    • Constantin Berhard says:

      Unicode in Python 2 was slightly tricky, Unicode in Python 3 is a breeze. I don’t understand why anyone would think otherwise.

  2. David says:

    This article is a pretty good summary of the differences and advantages of each. I agree with the conclusion, both have their strengths, use the right tool for the job at hand. This is true no matter which languages you are considering.

  3. John Monteiro says:

    Gunjit, you made no mention of modern OO Perl development techniques. Nobody codes what we call “classic” Perl modules anymore.

    • Gunjit Khera says:

      Yes, you mentioned it right, perl 5 introduced OO Programming.
      Thanks for updating us and sharing your knowledge with us and other readers.
      Stay Connected :-)

  4. Constantin Berhard says:

    This article leaves out very important design flaws in the Perl language, which can very easily lead to absurd security vulnerabilities, if you’re not 100% careful all the time.

    Look at this presentation:
    https://media.ccc.de/v/31c3_-_6243_-_en_-_saal_1_-_201412292200_-_the_perl_jam_exploiting_a_20_year-old_vulnerability_-_netanel_rubin

    Furthermore listing small performance gains (which I didn’t benchmark; did you?) between two SCRIPTING languages is totally pointless. It is like comparing two dish washers regarding their ability to knit a scarf. If you need performance for something, code (at least that critical part) in a compiled language like C or Rust.

    • Gunjit Khera says:

      Yes, but certain use cases favor the use of perl and there these minor performance gains matter, like regular expressions matching. Although i agree that as a scripting language, if you need performance you have lots of other options like: C and Rust as you pointed out or some other like: Go which can enhance concurrency when we talk of programming web servers , but Perl’s major advantage is its closeness to Linux and its tools i.e. awk and sed and its one liners where it can give a better performance.

    • Joe Chakra says:

      Actually performance does matter even for scripting. Imaginew filtering a 100 MB debug log. You could use AWK or gawk, sed or grep but Perl gives a lot more flexibility. Taking 5 seconds is very different to taking ten seconds, just because the more time between request and response the more likely you are to get distracted.

  5. djf says:

    perl is absolutely the finest – most wonderful of all options – and it makes programming languages “great again”. Everybody but python programmers know that ;-)

  6. rclark says:

    I would pick Python most every time over Perl if given a choice. Why? Readability and ‘ease of use’ trumps everything else… KISS principle at work… A few more lines to do the same job is worth it if you can ‘read it’ at a glance. For what we do, Python 3 does all we need for tasks we require of it whether reporting by writing Excel spreadsheets, or CSV, or XML or…., or networking, or simpile GUI with QT4 or Tk. In fact one of the things I was tasked at my company to do is replace the Perl scripts that were written before hand with Python replacements, so that the next guy (whether a programmer like me, or an Electrical Engineer) that comes along can easily understand what is going on if changes need to be made. The little speed you might get from Perl is inconsequential for what we are doing (pick right tool for the job of course). As for ‘ regex’, Python comes with the module ‘re’ (import re) if you need that capability. I don’t feel we have lost anything by dropping Perl and gained everything by embracing Python IMHO.

    • Gunjit Khera says:

      Thanks for sharing your views, these would be very helpful for our readers also who might be confused on choosing one of the two languages.
      Also Thanks for being a follower of tecmint, Stay Connected :-)

  7. Baker says:

    Perl is almost dead in the modern sysadmin/devops arena. Ruby is the new Perl. An article on Ruby vs Python will possibly get more hits.

  8. D. B. Dweeb says:

    Your regex example is biased.

    Consider Python:
    print re.findall(‘\d+’,str)

    Versus Perl:
    print “$& \n”

    If you look at it from the perspective of the uninitiated, it’s very obvious which one is easier to understand!! “$& \n” is UGLY!

    • Gunjit Khera says:

      Although $& seems ugly but with course of time a perl programmer can get practice of such syntax, although same would be with the python one, but main thing here would be that perl is more powerful in regex as it is inspired by and also inspired to run on linux OS, which makes it have an upper hand on python for pattern matching.

      • D. B. Dweeb says:

        Yes, but I asked you to look at it from the perspective of the uninitiated. While regex is a powerful tool, there’s WAY more involved with systems engineering than mere string manipulation. There are several Python web server development tools which use high performance regex for routing as a core function. Properly written Python regex routines can perform very well. If your usage of Python regex threatens the viability of your app then I’d say you have a design issue. IMHO.

  9. D. B. Dweeb says:

    The Pythonic file handling below surpasses the Perl example, the exception text and file close is automatic. Advantage Python!

    with open(“data.csv”) as f:
    for line in f:
    print line,

    • Gunjit Khera says:

      Yes, Thanks for sharing another important point i.e. file handling , this would surely advantage our readers.
      Stay Connected :-)

      • D. B. Dweeb says:

        Well it’s not really file handling, it’s understanding and using context managers. You were presenting a conclusion based on sub-optimal Python code! I will “stay connected” if you grant my point that in this specific example of yours it’s advantage Python.

  10. Wikihat says:

    I have read this article entirely and now I could say python will be best over a Perl ,it may third party but doesn’t matter and the matter is able to support most of the technology .

    • Gunjit Khera says:

      Thanks for sharing your view about the debate. It would surely be helpful
      Thanks for being a follower of tecmint, Stay Connected :-)

    • Constantin Berhard says:

      Nothing about the re module is third party. It is part of python. You have to explicitly import it just to avoid name collisions. It allows you to have a variable name that is "re" in code which doesn’t use regular expressions at all.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.