perl Command in Linux

Introduction

In this lab, you will discover how to leverage the Perl programming language within a Linux environment. We'll start with an introduction to Perl and the creation of a basic script. Then we will explore methods for executing Perl scripts from the terminal, before delving into practical demonstrations of Perl commands used for file manipulation. This lab offers you hands-on experience with core Perl programming concepts and shows how the language is applied in real-world Linux systemadmin tasks.

Introduction to Perl Programming in Linux

In this section, we'll introduce the Perl programming language and demonstrate its use within a Linux environment. Perl stands out as a robust and adaptable scripting language, making it a go-to choice for system administration, text processing, and web development.

Let's begin by verifying the installed Perl version on our Ubuntu 22.04 Docker container:

perl --version

Example output:

This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
(with 44 registered patches, see perl -V for more detail)

Copyright 1987-2019, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

As displayed, Perl version 5.30.0 is present in our environment.

Now, let's craft a simple Perl script. Generate a file named hello.pl within the ~/project directory:

nano ~/project/hello.pl

Populate the file with the subsequent Perl code:

#!/usr/bin/env perl

print "Hello, Perl!\n";

Save and close the nano editor.

Grant the script execute permissions and execute it:

chmod +x ~/project/hello.pl
~/project/hello.pl

Example output:

Hello, Perl!

We've successfully introduced the Perl programming language and walked through the steps to create and run a basic Perl script within a Linux environment. Further steps will delve into more complex Perl commands and practical examples.

Executing Perl Scripts in the Linux Terminal

In this segment, we'll understand the process of executing Perl scripts within the Linux terminal.

Let's begin by creating another Perl script, named math.pl, within the ~/project directory:

nano ~/project/math.pl

Insert the following Perl code into the file:

#!/usr/bin/env perl

$a = 10;
$b = 5;
$sum = $a + $b;
print "The sum of $a and $b is $sum\n";

Save the file, then exit the nano editor.

Next, grant execute permissions to the script, and then run it:

chmod +x ~/project/math.pl
~/project/math.pl

Example output:

The sum of 10 and 5 is 15

Perl scripts can also be executed directly using the perl command:

perl ~/project/math.pl

This approach will yield the same output as before.

Additionally, your Perl scripts can receive arguments via the command line. Adapt the math.pl script to accept two arguments:

#!/usr/bin/env perl

$a = $ARGV[0];
$b = $ARGV[1];
$sum = $a + $b;
print "The sum of $a and $b is $sum\n";

Save the file and run the script with arguments:

~/project/math.pl 20 10

Example output:

The sum of 20 and 10 is 30

This section detailed how to execute Perl scripts within the Linux terminal, covering both scenarios: making the script directly executable and employing the perl command for execution. We also addressed how to provide arguments to a Perl script via the command line.

Practical Examples of Perl Commands for File Manipulation

In this section, we will showcase practical uses of Perl commands for manipulating files.

To start, create a sample text file, data.txt, in the ~/project directory:

echo "Name,Age,City" > ~/project/data.txt
echo "John,30,New York" >> ~/project/data.txt
echo "Jane,25,Los Angeles" >> ~/project/data.txt
echo "Bob,35,Chicago" >> ~/project/data.txt

Let's now employ Perl to read and manipulate the contents of this file.

To read the file's contents and print each line:

perl -e 'while (<>) { print; }' ~/project/data.txt

Example output:

Name,Age,City
John,30,New York
Jane,25,Los Angeles
Bob,35,Chicago

To extract particular fields from each line (for example, name and city):

perl -lane 'print "$F[0], $F[2]"' ~/project/data.txt

Example output:

John, New York
Jane, Los Angeles
Bob, Chicago

To substitute a specific value inside the file:

perl -i -pe 's/New York/San Francisco/' ~/project/data.txt

Now, confirm the file's contents:

cat ~/project/data.txt

Example output:

Name,Age,City
John,30,San Francisco
Jane,25,Los Angeles
Bob,35,Chicago

This section explained how to utilize Perl commands to read, extract data from, and modify text files. These examples illustrate Perl's versatile capabilities in handling file manipulation tasks, essential for any systemadmin working in Linux.

Summary

This lab began with an introduction to the Perl programming language and its applications within a Linux context. We covered how to check the installed Perl version, write a simple Perl script, give it execute permissions, and then run it. We then advanced to executing Perl scripts in the Linux terminal, showing how to pass command-line arguments and leverage the shebang line for direct script execution. Finally, we delved into practical examples using Perl commands to manipulate files, including reading, writing, and editing files. By completing these steps, you should now possess a foundational comprehension of Perl programming and its relevance within a Linux system.

400+ Linux Commands