p

python tutorial - Python Vs Perl - learn python - python programming



Often when a new programming language is introduced, there is a debate which starts amongst some of the genius minds in the industry wherein the language is compared with the one already spreading its roots.

  • Many examples of such cases can be picked up and investigated from past including the debate between Java and C#, C++ etc.
  • One of such case which drew a significant amount of attention was the debate between two languages which came out one after the other in 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.
  • 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 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:

  • Both targeted Unix Operating System, one for hackers and other to process reports.
  • 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
  • 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 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 huge advantage over Perl when it comes to code readability. Python’s code is lot more cleaner 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 more cleaner. On 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.
  • Apart from this, with ‘{‘ and ‘}’ representing a block of code and unnecessary addition of ‘;’ at 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 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 digit in the string in Perl and Python.

Python

Import re
str = ‘hello0909there’
result = re.findall(‘\d+’,str)
print result
click below button to copy the code. By Python tutorial team

Perl

$string =  ‘hello0909there’;
$string =~ m/(\d+)/;
print “$& \n”
click below button to copy the code. By Python tutorial team

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 object OOP in Perl being outdated where package is used as a substitute for classes.
  • 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 other hand Perl is best for its one liners which can be used on command line for performing various number of tasks. Also, Perl code can eventually do various tasks in less lines of code than python.

A short code 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
click below button to copy the code. By Python tutorial team

Perl

open(FILE,”%lt;inp.txt”) or die “Can’t open file”;
while(<FILE>) {
print “$_”; } 
click below button to copy the code. By Python tutorial team

Pros and Cons – Python vs Perl

Python PROS:

  • Has a clean and elegant syntax which makes this language a great choice as first programming language for novices who want to have a hands-on on any programming language.
  • Has very advanced and inherent OO Programming, also thread programming in Python is way better than Perl.
  • There are many application areas where Python is preferred and even it outperforms Perl. Like: Perl is preferred for CGI scripting but now a days Python’s Django and web2py like web scripting languages are becoming more popular and have huge attraction from the industry.
  • 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.
  • 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.
  • Python is good for various applications like: Big Data, Infra Automation, Machine Learning, NLP etc, is has huge support of active communities because of being Open Source.

Python CONS:

  • There are few areas where execution in Python is usually slower than that of Perl including: regex and string based operations.
  • 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 type of the variable which gets hectic and complex.

Perl PROS:

  • Perl has powerful one liners and even ensures UNIX piping like syntax which can be used on 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.
  • Perl is known for its powerful regex and string comparison operations as it is influenced by sed and awk like powerful UNIX tools. In case of regex and string operations like: substitution, matching, replacement, Perl outperforms python which would take few lines of code to achieve the same. Also many file I/O operations, exception handling are done faster on Perl.
  • When it comes to a language for report generation, Perl has always been in the fame since its introduction as one of the main reasons for author to develop language like Perl was for report generation.
  • 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.
  • It is easy to identify type of variable with the symbols that Perl uses before them, like: ‘@’ identifies arrays and ‘%’ identifies hashes.

Perl CONS:

  • Perl has 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.
  • OO Programming in Perl is a bit out of date as it has never been known for OO programming and many operations like: threading is also less pronounced on Perl.

Related Searches to Python Vs Perl