Categories
Perl

Why Perl is my favourite programming language in 2021?

Perl started it’s development in 1987 before the born of the Linux kernel and gained popularity as a web development language for CGI scripting during the 90s since then many developers have categorized the language as a write only language, but I think this opinions are deprecated with the today features of modern Perl plus the expressiveness Perl have been given to it’s developers since it’s starting.

Perl has three important types of variables, each of one features a sigil which must be before any representation of that variable.

$ features a Scalar variable, a variable which can hold only a element.

@ features an Array variable, a variable that holds a list of elements.

% features a Hash variable, a variable that holds a map of String to Scalar without order.

This differentiation plus the usage of “use strict;’ makes the development of Perl easier since many complex data type errors can be caught on compile time.

Perl is a dynamic typed programming language as Python or Javascript, so you can assign a Scalar variable any value like a String, a Number, Undef (Perl has no null.) or a Reference to a more complex type of variable.

By default Perl interprets that if you are passing a Array or Hash to a Subroutine what you want is passing the contents of this structure as the parameters of the Subroutine which makes some constructs longer in some programming languages to be written without hastle.

Let’s put it in a example:

use strict;
use warnings;

use feature 'say';

sub ExampleSubroutine {
    my ($a, $b, $c) = @_;
    say $a;
    say $b;
    say $c;
}

my @params = ('a', 'b', 'c');
ExampleSubroutine(@params);

The lack of need to specify signatures to subroutines allows a lot of expresiveness also, althought you may use libraries like https://metacpan.org/pod/Params::ValidationCompiler in more complex programs to get a self-documenting interface to your methods and subroutines.

Testing in Perl is also great thanks to great resources from cpan like https://metacpan.org/pod/Test::MockObject, https://metacpan.org/pod/Test::MockModule and https://metacpan.org/pod/Test::Most and the flexibity of Perl on mocking subroutines that allows to test both procedural and OOP in a really easy way no matter there was not test before unlike PHP where mocking it’s limited to object methods.

Tons of the Perl cpan modules are packaged in GNU/Linux distributions so if you want to package your software to work in a GNU/Linux distribution it is not a so difficult task.

Perl also grants its developers a great UTF-8 optionally aware regex engine which helps to parse complex data in a easy manner and it’s the most common way to manipulate Strings in Perl.

The Perl object’s are just blessed references which allows your object to be a Hash (The most common since allows the common key/value internal scheme of an Object.), an Array, an Subroutine (To force encapsulation by having the real attributes in a more restricted scope.)

Perl anonymous subroutines inherit the scope allowing to do some functional programming tricks.

There are great libraries to work with files in Perl like https://metacpan.org/pod/Path::Tiny which allows a near natural language way to manipulate files without having to be so aware about file handles or directory handles as the default. (The default sometimes also plays well in scripts.)

The Perl command line debugger is simply great, allowing to discover bugs in a easy and reproducible way so you can share debugging sessions as plain text with other programmers.

The default variable $_ allows the programmer to not to be having to name a variable that is used in a very limited scope before stopping being useful.

The for syntax is really cool $object->doSomething for my $object (@objects) or it’s anonymous counterpart $_->doSomething for (@objects)

It’s not always needed to put parents on subroutine calls allowing the developer to type less when the scope is clear.

It is also great the “There is more than a way to do it” philosophy behind the language, allowing you to choose the best way to do for your problem.

When we talk about optimization we should first know for what we want to optimize like Speed, CPU, Memory Usage or code Legibility it’s not uncommon you have to trade one to have the other, for example a complex and extensive data structure can be stored in disk to save memory but accesing it will harm Speed, CPU usage and Legibility by increasing the latency because disk reads, increasing the CPU usage because the aditional cycles to retrieve the data when needed and Legibility by doing the program more complex.

It’s is great a language takes different problems have different needs and adapt itself to the multiple ways to tackle a problem a developer may have.

By sergiotarxz

I am a software developer with high interest on free software.

Leave a Reply

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