When To Choose Python Over Perl (And Vice Versa)?
7
10
Entering edit mode
10.6 years ago
Niek De Klein ★ 2.6k

I have no experience with Perl and therefore always choose to use Python. So far I've heard that they're in basics the same and that a project being programmed in Perl or Python is usually decided what the group/person starting the project is most comfortable with. I am wondering what the ups and downs are for choosing one over the other.

For those that are proficient with both, why/when do you choose Python over Perl?

python perl comparison • 109k views
7
Entering edit mode

Perl for one-liners in a pipe. Python for everything else.

5
Entering edit mode

Oh no! I anticipate a nearly religous battle with Perl-monks vs. Python-zealots.

4
Entering edit mode

There are probably specific cases where one is better than the other, but in my fairly general use, the overriding factor is to use the one your co-workers will be able to read.

2
Entering edit mode

In commandlinefu, there are 27 commands tagged with python and 120 with perl - but python -m SimpleHTTPServer is the #2 best one-line command: http://www.commandlinefu.com/commands/browse/sort-by-votes

0
Entering edit mode

It sometimes depends on the packages/libs available for your purpose of each language.

0
Entering edit mode

@Michael Schubert

I disagree. python -c enables command line access to python, and therefore makes perl completely irrelevant.

0
Entering edit mode

I know the -c flag, but with imports it gets cumbersome quickly.

0
Entering edit mode
use strict;
use warnings;

32
Entering edit mode
10.6 years ago

When to use perl:

• whenever you have to write a quick and dirty script that uses regular expressions or that executes system commands. Perl has been designed for automating operations on files and file systems; in fact, in perl to use regular expressions or to call system commands you don't need to import anything, while in python, you need to import a library to do these operations.
• perl programmers usually adopt the Unix Philosophy of "Make one tool that does only one specific task". This is the reason why most perl script don't even include functions, not to speak of objects. Each perl script must be written in order to execute a single task, and then the scripts are piped together through the Unix piping system. However, consider that this approach can lead to a lot of confusion, and is generally difficult to mantain. Moreover, you can do "One tool one task" in python, too; the only difference is that you will have to import sys in every script.
• whenever you need to use a library available only for perl. Have a look at the modules in CPAN. I think that BioPerl is still slightly more complete than BioPython, and that there are more bioinformatics-oriented modules for perl than for python (unfortunately). The Ensembl APIs are also available only for perl.

When to use python:

• whenever you need to use the script more than once.
• whenever there is a even remote possibility that one of your colleagues or someone else will ever use your script.
• whenever you need to use functions or objects. As I was saying before, the majority of perl scripts don't even include functions, because they are more difficult to write than in python. For example, in perl, you need to learn how to pass the reference to a variable, etc.. and this leads to scripts that are more difficult to understand (in perl).
• if this is the first programming language you are learning, I suggest you to start with Python. Python is much cleaner than Perl, and is designed to respect some good practices that any programmer should know. Have a look at the Python Zen for some ideas.

## Edit after 5 years:

• Python has now support for tabular-like data structure (data frames), which are very important for data analysts. Use Python or R whenever you have to work with tables
• Python has now excellent support for machine learning algorithms and data analysis in general. Use Python or R whenever you need to do one of these things.
0
Entering edit mode

Best explanation I've ever seen; you give concrete examples and explain both fairly.

1
Entering edit mode

Though we need to factor in that this answer was given 5 years ago.

Today there are even fewer reasons for using Perl - you'd be missing out many advancements and developments in other languages. Frankly, as much as I want to stay away from Perl vs X arguments I could not honestly recommend anyone to learn Perl as their first language.

0
Entering edit mode

I agree. Nowadays it is more Python vs R rather than Perl.

23
Entering edit mode
10.6 years ago
Asaf 8.7k

After about 5 years of using Perl I've moved to Python. I can't really think of something I can do in Perl but not in Python. Python, in my opinion, is much cleaner than Perl and have the great advantage of being object oriented. Building modules is much easier in Python hence the code you write is more reusable.

11
Entering edit mode
10.6 years ago

I think very few people are completely agnostic about programming languages especially when it comes to languages with very similar strengths and weaknesses like: perl/python/ruby - therefore there is no general reason for using one language vs the other.

It is more common to find someone equally proficient in C and perl, than say equally proficient in perl and python. My guess would be that complementary languages require complementary skill sets that occupy different parts of the brain, whereas similar concepts will clash more easily.

7
Entering edit mode
10.6 years ago

You are totally correct with your initial assumption. This question is similar to choosing between Spanish and English, which language to choose? Well if you go to Spain,...

All (programming) languages are equal, in the sense of that you can solve the same class of problems with them. Once you know one language, you can easily learn all imperative languages. Use the language that you already master or that suits your style. Both (Perl & Python) are interpreted languages and have their merits. Both have extensive Bio-libraries, and both have large archives of contributed packages.

An important criterion to decide is the availability of rich, stable, and well maintained libraries. Choose the language that provides the library you need. For example, if you want to program web-services (using SOAP not web-sites), you better use Java or maybe C#.

Conclusion: it does no harm to learn new languages. And no flame wars please.

6
Entering edit mode
10.6 years ago
Andrew W ▴ 290

Python forces you to write cleaner code and gets you thinking about objects from the beginning, which should make the transition to object-oriented coding easier. If someone is new to programming, Python is likely the better choice. But that doesn't mean people, especially beginners, will automatically write code that can be easily understood by others. Perl only makes it easier for people to write confusing code.

If someone is already a competent Perl programmer, I don't think there's any overwhelming reason to switch. Most employers will understand that someone competent in one of those languages will quickly pick up the other. The time may be better invested learning Java or R scripting.

0
Entering edit mode

Last time I checked it didn't force you to write OO.

0
Entering edit mode

Thanks, my mistake. I've updated with a more accurate statement.

0
Entering edit mode

use strict; use warnings;

4
Entering edit mode
10.5 years ago
John Kern ▴ 60

Hello,

Like bioinformatics, many other areas share the problem of dealing with "big data" and Data Mining. In industry and computer science, Python (and Java) is more common than Perl in these areas. You may find programming in Python beneficial because you would be able to leverage the work and infrastructure of others in related fields.

-HTH John

3
Entering edit mode
10.5 years ago
Aditya Joshi ▴ 30

Hi ! Here's a list of few areas which are important from point of view of Bioprogramming and the winner among Perl and Python in each :-

1) Ease of programming : Python[?] 2) Regex use and speed of execution of engine : Perl (Python will soon catch up with its 3.x)[?] 3) Bio* packages : Are available for both for Python and Perl, though here Python is the clear winner due to its inherent OOP philosophy.[?] 4) Syntax : Python any day (God, I don't know why people use Pseudocode when there's Python!). a geek joke says : "Perl is the only language whose code looks same before and after RSA encryption"[?] 5) Wrappers : Python has excellent SWIG wrappers for cross-language implementation of code. All popluar languages : C, Java and .NET have a combined distro with Python (Cython, Jython, IronPython etc.)[?] 6) Web programming : Ah! Here comes the most useful thing for Bioprogrammers, Perl CGI or Python ? With almost all bioinformatics knowledge housed in online biological databases, and with computing done by online tools, the choice of language for this one is important. I am once again not being biased towards Python, but apart from CGI functionality in both, Python's web frameworks are better than that of Perl (google these : web2py, Django etc). With these the development time gets reduced to half. Also, Python has an enhanced functionality in the form of ModPython and its even faster relative : ModPython Publisher. It gives performance as fast as PHP and even a C-CGI code.[?]