What is Perl?
Perl is a general purpose, interpreted programming language that excels at text processing and manipulation. Created by Larry Wall in 1987, Perl has been described as the "duct tape" that holds the internet together due to its widespread early adoption for CGI scripting.
Some key features that make Perl popular:
- Permissive licensing allowing reuse and modification
- Very powerful regular expressions baked into the language
- Extensive code libraries CPAN hosting over 180,000 Perl modules
- Fast processing and short development times suiting DevOps
- Runs on all major computing platforms like Windows, Linux, macOS
Perl continues to be used today for bioinformatics, network programming, finance applications, system administration tasks and more.
Installing Perl on Ubuntu
Let‘s go through the simple process of installing Perl 5 on Ubuntu using the apt package manager.
First update apt‘s package index and upgrade any available packages by running:
$ sudo apt update $ sudo apt upgrade
Now install Perl with all dependencies:
$ sudo apt install perl
This will install the latest Perl version available in Ubuntu‘s repositories along with required libraries.
To verify, check the installed version:
$ perl -vThis is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
We can also look at all the Perl related packages present:
$ apt list --installed | grep -i perllibalgorithm-diff-perl/focal,now 1.19.03-2 all [installed] libalgorithm-diff-xs-perl/focal-updates,focal-security,now 0.04-6ubuntu0.1 amd64 [installed] libalgorithm-merge-perl/focal-updates,now 0.08-3ubuntu0.20.04.1 all [installed] ..... perl/focal-updates,now 5.30.0-9ubuntu0.2 amd64 [installed] perl-base/focal-updates,now 5.30.0-9ubuntu0.2 amd64 [installed] perl-modules-5.30/focal-updates,now 5.30.0-9ubuntu0.2 all [installed]
This confirms Perl 5.30 and related libraries are installed properly.
Hello World Perl Script
Let‘s create a simple Perl script that prints "Hello World!".
Use your preferred editor to create a file named hello.pl with the following contents:
#!/usr/bin/perluse warnings; use strict;
print "Hello World!\n";
The shebang line tells Linux to use the perl interpreter to execute this script. We import common pragmas for security and robustness.
The actual program just prints our greeting. Save the file and exit editor.
Make this Perl script executable and run it:
$ chmod +x hello.pl $ ./hello.pl Hello World!
Our Perl program executed and printed Hello World! It shows Perl is installed correctly.
Let‘s go over the basics of what this Perl script is doing…
Anatomy of a Perl Script
- #!/usr/bin/perl defines path to perl interpreter
- use warnings turns on warnings for safer code
- use strict enforces cleaner code discipline
- print prints output stream
- \n escapes a newline
These simple building blocks allow creating Perl programs for all kinds of tasks. Let‘s look at some more real-world examples.
Using Perl for System Administration
Perl is commonly used for sysadmin tasks in Linux environments. Its low overhead, easy text processing and rapid prototyping capabilities make Perl a favorite among system administrators.
Some examples include:
- Log file analysis – Perl one-liners can analyze huge logs
- Bulk system configuration changes using regular expressions
- Scripting recurring tasks like backups, report generation etc
- Web scraping and automation for gathering system data
- CGI and PSGI based web admin dashboards and monitoring
Let‘s take a simple example of using Perl to find the top 10 longest running processes on a Linux server:
$ ps aux --sort -etime | head -11 | tail -10 | perl -lane ‘print $F[0]‘
This pipelines Linux process data, sorts by elapsed time, extracts top 10 rows, retains only the process info field and prints with Perl.
As you can see Perl one-liners can enable powerful administration capabilities to Linux sysadmins.
Using Perl for Web Development
Perl offers great capabilities for web development thanks to its text manipulation capabilities.
Some examples:
- CGI programming allowing dynamic server-side web apps
- Frameworks like Dancer, Mojolicious for building web apps and services
- Web scraping scripts to gather data from websites
- Convert data formats like XML, CSV, JSON for web services
- Image processing for dynamic image generation
Here is an example using Perl CGI to create a simple web form:
#!/usr/bin/perluse strict; use warnings;
print "Content-type:text/html\r\n\r\n";
print ‘‘; print ‘‘; print ‘
Sample CGI Form ‘; print ‘‘;print ‘‘;
print ‘‘; print ‘‘;
print ‘‘; print ‘‘;
print ‘‘;
print ‘‘;
This prints a HTML page with a text box and submit button. Form data is sent back to /cgi-bin/sample.cgi program which can process it with Perl CGI libraries.
As you can see Perl offers a easy way to build dynamic server-side web applications.
Using Perl for Research
Perl is extensively used in academic and scientific research programming.
Some examples include:
- BioPerl project for genomics, proteomics and biology analysis
- Natural language processing research in computational linguistics
- Statistical analysis and data visualization for research
- Astronomy data analysis processing telescope imagery
- Workflow automation and prototyping of experiments
Perl supports integration with specialized research equipment using techniques like:
- Database connectivity to gather experimental data
- Interface with lab hardware through network sockets or RS232 connections
- Data format parsing modules e.g. BioPerl handling genomics data
- Statistics and machine learning modules like PDL
This combination of text processing capability and bindings for scientific programming makes Perl a common sight in research programming.
Integrating Perl Programs
Perl offers great ways to integrate and connect various systems thanks to CPAN hosting over 18,000 modules.
Some examples include:
- DBI and DBD allowing database connectivity to MySQL, PostgreSQL etc
- SOAP, REST modules to connect with web services
- Framework connectors e.g. web app frameworks like Django
- Network socket libraries for interfacing with remote systems
- Interprocess communication via pipes, message queues
- Operating system integration with WMI on Windows or /proc on Linux
This small sample of CPAN modules shows how effectively Perl integrates external data sources, APIs, services and platforms.
Comparing Perl to Other Scripting Languages
How does Perl compare to other popular scripting languages like Python, Ruby, PHP or Node.js?
- Python known for general purpose use, web dev, scientific computing
- Ruby focusses on programmer happiness and web development
- PHP built for server-side web development and content management
- Node.js uses JavaScript for highly scalable web apps and real-time services
Some differences in comparison:
- Perl has the most advanced regular expressions built-in
- Python syntax dijo considered cleaner by many programmers
- Ruby has a greater emphasis on developer experience
- PHP has widespread use in web hosting environments
- Node.js offers asynchronous I/O for real-time reactivity
Perl has influenced all these languages, evident in syntactic similarities. While other languages have gained ground in specific niches, Perl continues to be useful for its fast text processing, small footprint, vast CPAN resources and lightweight threading – making it especially popular in instance provisioning and configuration management automation.
Conclusion
Perl is an established scripting language well suited for text manipulation, system administration, web development and more. As we saw, we can easily get started with Perl on Ubuntu Linux using apt.
Within a few installation and configuration steps, Perl can be used for writing scripts to accomplish automation tasks. It serves as a great tool to have in a developer‘s toolbox along with other languages. Contributions continue with recent major versions showing Perl is still well maintained.
CPAN hosting over 180,000 modules makes almost any task possible using Perl. Integration with databases, web technologies and specialized libraries allows building robust programs.
For its strong text processing capabilities, widespread modules and quick prototyping abilities, Perl continues to help get the job done on Linux and other platforms.