Exploring the Differences: Python vs. Perl in 2023

Where does this heated debate stand in 2023?

By: R. Paulo Delgado
August 19, 2023

A comparison of Python with Perl in 2023 looks much different than a comparison of the two programming languages did in the 1990s and early 2000s. Back then, Perl was one of the most powerful scripting languages for web development. But fast forward 20 years, and Python has clearly won the popularity race. Some people have even started calling Perl a dying language.

Still, Perl has features that make it superior to Python—or any other language—in certain tasks. Many legacy systems are also written in Perl, and changing them isn’t a viable option.

In this article, we’ll look at real-world use cases of Python vs. Perl in 2023 and where you might legitimately choose one over the other.

Today, Python has over 17 million active developers, while Perlhas only 1 million. One survey found that a mere1% of all programmersused Perl in the past 12 months.

Python’s popularity is fueled by an easy-to-read syntax and its user-friendliness as a web development language. The web is a significant driver of programming language popularity, as shown by how many people use JavaScript, currently the most popular programming language in the world, driven by popular web development frameworks such as Vue and Angular.

Web development also drove Perl’s popularity in the late 1990s and early 2000s. Booking.com, IMDB, and DuckDuckGo all still use Perl extensively. Booking.com isespecially activein the Perl community.

Perl, however, is far from user-friendly. Perl’s official motto is “There’s more than one way to do it,” often represented by the acronyms TMTOWTDI or TIMTOWTDI. This philosophy makes it extremely challenging to use Perl in large projects because the same code can be written in many different ways. Team members might struggle to understand other programmers' work, makingcode reviewsdifficult and opening the door up to more bugs.

But Python prefers a standard way to write code. This makes Python far easier to learn, and better-suited to team projects

For example, looping through a list of items in Perl could be written in any of the following ways:

Perl (Option 1)

my @list = (1, 2, 3, 4, 5); foreachmy $item (@list) { print $item; }

Perl (Option 2)

my @list = (1, 2, 3, 4, 5); formy $index (0 .. $#list) { my $item = $list[$index]; print $item; }

Perl (Option 3)

my @list = (1, 2, 3, 4, 5); for (my $index = 0; $index < scalar @list; $index++) { my $item = $list[$index]; print $item; }

Perl (Option 4)

my @list = (1, 2, 3, 4, 5); for (my $index = 0; $index <= $#list; $index++) { my $item = $list[$index]; print $item; }

Perl (Option 5)

my @list = (1, 2, 3, 4, 5); my @processed_list = map { my $item = $_; print $item; } @list;

In Perl, arrays are represented by an @ before the variable, but in Option 2 above, we use a $# prefix, which accesses the index of the last element of an array.

We use $# in Option 4 but use the keywordscalarin Option 3. Thescalarkeyword returns the number of elements in an array, so we have to change the < (less than) to <= (less than or equal to) when choosing betweenscalaror %# so thefor loopdoesn’t access an out-of-index member.

If you’re getting confused, that’s the point. Perl wasn’t designed to be readable but to be flexible. This makes it challenging to use Perl in a team context. Individual programmers might feel empowered because they have many choices for personal scripts, but maintaining a team project becomes challenging when there are so many ways to do the same thing.

If you’d like to try the above examples, you can test them using thisonline Perl interpreter.

In Python, there’s onlyoneway to do the above.

Python

list = ["a", "b", "c", "d", "e"] for item in list: print(item)

Python is unique from other languages because it doesn’t have aforeachconstruct. Itsforloop works for bothforeachand index-based looping, increasing its simplicity.

If you’d like to try the Python examples in this article, you can use thisonline Python interpreter.

Python and Perl’s origins

Python and Perl were built for distinctly different needs. Perl was developed as a data and text processing language, and as a tool for system administrators, whereas Python was created as a general-purpose language meant to be easy to learn.

Python’s user-friendliness and broader scope have contributed significantly to its popularity.

Perl and Python were initially released at around the same time: Computer programmer Larry Wall released Perl in 1987, and Dutch programmer Guido van Rossum released Python in 1991. But Python has morphed considerably since its initial launch, and now embraces far more than it did initially. Python 3, released in 2008, brought further significant changes to Python that turned it into a first-class citizen for web development.

Some of the features and improvements that Python 3 introduced include:

  • Python 3 handles all strings natively as Unicode, whereas Python 2 handles them as ASCII. This is huge for web development, where UTF-8 is the favored encoding.

  • Python 3 further simplifies and unites the Python syntax.

  • Python 3 is faster.

  • Python 3 introduced several new libraries, including several libraries that enhance a programmer’s ability to code web applications.

Some of the Python 3 libraries introduced include:

  • asyncio:Provides native support for asynchronous programming, crucial for web programming.

  • enum:Lets programmers work with enumerations, which makes code more readable.

  • pathlib:A powerful library for working with file systems, regardless of the underlying platform, also essential for web development.

  • zoneinfo:Introduced in Python 3.7, this library lets users work seamlessly with timezone information, another vital aspect of web development.

The popular web frameworkDjango, released in 2005, also contributed significantly to Python’s use as a web development language.

Although Perl underwent significant changes between versions 1 and 5, it stayed focused on its area of expertise: text manipulation and quick scripting for systems administrators. Using Perl, system administrators could create simple scripts that work with the underlying operating system in just a few lines of code. But even there, Python is catching up. Creating system scripts in Python generally requires a few more lines of code, but the code is far more readable.

Perl is the winner for text processing

Perl shines in text processing and report generation. Its speed in this respect is unmatched.Perl performs up to 20 times faster比Python为某些文本操作的动作。

One test showsPerl running eight times faster than Pythonfor a moderately sized data set. Perl is an excellent option for companies that need to crunch gigabytes of text data.

Perl usesregular expressions(regexes) extensively. Regular expressions is a field of programming focused on string and text processing. By using predefined “metacharacters,” regular expression engines can easily extract and modify large amounts of text.

Virtually every major programming language today has support for regular expressions, whether inherently or as part of a regex library. Perl differentiates itself in that regular expressions are deeply integrated into the language itself, not requiring additional libraries.

Python的正则表达式功能ity is also extensive but far more readable. Python is the better choice for small text-file operations from the standpoint of readability.

Consider the following two examples, where we use regex to substitute a set of characters in a string:

Perl

my $string = "Hello, World!";# Binding substitution operator (s///)$string =~ s/World/Perl/; print"Modified string: $string";

Output:

Modified string: Hello, Perl!

Python

import re string = "Hello, World!"# Substitution operator (sub())modified_string = re.sub(r"World", "Python", string) print("Modified string:", modified_string)

Output:

Modified string: Hello, Python!

In Perl, we use the substitution operator (s///) to replace any instance of “World” in the variable. Because regexes are built into the language, we simply assign the value using the binding match operator =~.

The Perl substitution operator (s///) uses the syntax:

s/$find_pattern/$replacement_pattern/.

In Python, we need to import an external module,re, for regex support. The above example is simple, but we recognize quickly that the Perl code is less intuitive. The binding match operator (=~) is an unusual construct compared to other languages. Perl’s brevity is excellent for people with extensive experience in the language but challenging for newcomers.

Further adding to the Perl complexity is that the above snippet of Perl code could be written in several different ways. For example, Perl has a “default” variable ($_). The default variable is used if no other variable exists in the context. This cryptic concept leads to code as follows:

Perl regex using the default variable

$_ = "Hello, World!";# Binding substitution operator (s///)s/World/Perl/; print"Modified string: $_\n";

On line 3,s/World/Perl/stands on its own. Well, Perl is actually assigning that operation’s result to the default variable. Yes, we save a few characters by not writing the following:

$_ = s/World/Perl/

But the result is hard-to-read code that follows different standards from programmer to programmer. It can turn a large project into a mess.

Advanced regular expressions

Perl has a few highly advanced regex features that aren’t available in Python. One of those features is the ability to execute code inside a Perl regular expression.

In the following advanced example, we show how Perl can be used to:

  1. Match the first occurrence of a pattern

  2. Then replace the second occurrence of that pattern with string of our choice

my $string = "apple orange apple banana apple";# Named capture and replacementmy $count = 0; $string =~ s/(?apple)/ ++$count == 2 ? "replacement" : $+{fruit} /ge; print $string;

We use what’s called a “named capture” with the construct. Then we use the code++$count == 2inside the regular expression to check for the second time this pattern matches in the string. The regular expression replaces the second occurrence of “apple” with whatever we choose. In this case, “replacement.” The output is:

apple orange replacement banana apple

To replace every occurrence except the first one, we can modify the regex as follows, changing ++$count == 2 to ++$count > 1:

$string =~ s/(?apple)/ ++$count > 1 ? "replacement" : $+{fruit} /ge;

The output then becomes:

apple orange replacement banana replacement

This functionality is extremely powerful and can’t really be replicated in Python.

Some of the use cases for this advanced processing include:

  • Parsing and editing HTML files

  • Parsing legacy text files that don’t follow a specific structure (this is a common problem for legacy banking systems)

  • Processing log files

It’s possible to achieve a result similar to the above using Python, but it requires more lines of code, and the result is slower.

Python

import re string = "apple orange apple banana apple"# Named capture and replacementcount = [0] def replace_fruit(match): count[0] += 1 return"replacement"if count[0] == 2else match.group("fruit") pattern = r"(?Papple)" string = re.sub(pattern, replace_fruit, string) print(string)

Technically speaking, you can do anything in Python text processing that you can do in Perl. It might just require more code and workarounds, resulting in far slower code. The matter only becomes important when you regularly deal with enormous amounts of data.

Regular expressions is a challenging field. Regardless of the language you’re working in, considerbuying regex servicesto help you write and maintain regexes in your projects.

Python vs. Perl: Desktop software

Technically speaking, you could create desktop software using Perl. Various third-party tools and frameworks exist to make this possible, including:

  • GTK-2 Perl

  • Perl/Tk

  • Prima

  • Qt bindings for Perl

The real question is: Would you want to?

The frustrations of using Perl for modern applications are best expressed in a2007 blog postwritten by Bugzilla’s chief architect, Max Kanat-Alexander. Perl was already experiencing the cracks that led to its near abandonment today. Perl is an excellent programming language for people who already know Perl, but its complexity makes code maintenance challenging, especially for long-term projects.

In 2023, Bugzilla is still written in Perl. Kanat-Alexander’s blog sheds light on the enormous challenges of porting massive code bases to a new language, even if that new language makes sense. He writes,"I think that the experience of Netscape and the Mozilla Project shows us that re-writing Bugzilla from scratch and totally ignoring our old code base would be a bad idea."

他指的是有关的网景体验to an incident that’s become part of software development lore: In 1996,Netscape was the most popular internet browserin the world. It had muscled away market share from another browser called Mosaic. By 1998, however, Internet Explorer had taken 50% of the market share. During this critical time, Netscape decided to completely rewrite its codebase to address the browser’s failings.

It tookyears.

Well-known author and software engineerJoel Spolsky described the moveas"the single worst strategic mistake that any software company can make: They decided to rewrite the code from scratch."

By the time Netscape 6 was ready, Internet Explorer had taken over the market. Netscape was dead.

Spolsky wrote,"The idea that new code is better than old is patently absurd. Old code has been used. It has been tested. Lots of bugs have been found, and they’ve been fixed."

For new software, choose Python. But if you have a massive code base written in Perl, it might be best to stick with Perl, despite its complexities.

If you need help with your existing Perl tools, you canbuy Perl development servicesfrom Fiverr as needed.

Python for desktop development

Python is more widely known as a web development language, used by Spotify, Netflix, Facebook, Amazon, and many other large companies.

But Python is also an excellent cross-platform choice for desktop software. The Dropbox desktop app is famously built using Python.

To develop desktop apps in Python, you need to either use a third-party GUI framework or leverage Python libraries for creating GUIs, such aspywin32.

Some of the external GUI frameworks in existence include:

  • wxWidgets

  • Tkinter

  • Kivy

  • PyQt

Choosing Python over C++ or another desktop programming language means you only need to house one set of developers for both your web and desktop development.

Perl 6—Raku

Perl’s creator recognized Perl’s failings. When working on Perl 6, hesaid,"In Perl 6, we decided it would be better to fix the language than fix the user."The result was an entirely new language—Raku.

Although Raku is technically Perl 6, it isn’t backward compatible with Perl 5. Raku simplifies many of the language constructs in Perl but still uses many of the same keywords as Perl.

Perl’s keywords are unique and quite different from many of the popular languages. This adds an additional barrier to learning the language for existing programmers. Raku will likely become popular among existing Perl programmers who need the additional flexibility that Perl can’t give them, so they can expand into broader programming tasks. But Raku isn’t really Perl.

Perl 7 will continue to build on Perl 5.

When using Perl makes more sense

Here are a few edge cases that encourage learning Perl over Python in 2023.

Legacy code

If you’re working on a massive application written in Perl, it makes more sense to continue developing in that language rather than rewriting the entire codebase.

Debian developers and users

Perl is deeply integrated into the Debian operating system. The Debian team has gone out of its way to ensure that Perl scripts work seamlessly on Debian and provided features to efficiently resolve Perl dependencies.

Many Debian system scripts and tools are written in Perl, or use Perl extensively, such asdpkg, the Debian Package Manager, anddebconf, the Debian configuration management system.

Extensive regular expression experience

如果你已经广泛用Perl是有意义的regular expressions and the task you’re managing demands processing vast amounts of text. Perl’s power comes across clearly in its regex handling, and you can do things with a single regex expression that would take several lines in other languages.

Job security

AsPerl’s usage continues to decline, the need for Perl programmers to manage legacy systems will grow. This adds some degree of job security for highly experienced Perl programmers.

雇佣Fiverr Perl和Python程序员

The Fiverr marketplace has many Perl andPython programmers ready to help youwith your project’s needs.

招聘一个Perl自由职业者might be easier than taking on a new team member if you’ve got legacy systems and severely outdated codebases. As Spolsky writes, old code might be “ugly,” but it’s been thoroughly tested and works. Taking on a full-time team member to maintain legacy code is usually unnecessary.

Fiverr’s Safety Team ensures that all your transactions are secure and you receive the best possible service.

To find Perl or Python programmers on Fiverr, follow these steps:

  • Open a Fiverr account if you don’t have one already.

  • Search for “Perl developer” or “Python Developer” through the search bar.

  • Alternatively, browse the categories in the Programming and Tech section to find the services you’re looking for.

  • Look at the freelancer’s reviews and ratings. If the seller is new, read their profile and see their expertise and experience.

  • Contact the seller if you want to know more or have specific questions.

  • Place the order and provide the seller with all the info they need to work on your project.

Sign up for Fiverrtoday to get started!

About Author

R. Paulo DelgadoTech & Business Writer

R. Paulo Delgado is a tech and business freelance writer with nearly 17 years of software development experience under his belt, including WordPress programming. He is also a crypto journalist for Moneyweb, and proudly a member of Fiverr's Pro Seller program — hand-vetted professionals, verified by Fiverr for quality and service.