How to Install Perl on Rocky Linux EL9 or EL8

For developers and system administrators seeking a versatile programming tool, Perl is preferred. This high-level language, renowned for its text manipulation, web development, and system administration capabilities, offers many features tailored for diverse applications. If you aim to install Perl on Rocky Linux 9 or the older stable Enterprise Linux release of Rocky Linux 8, this guide is tailored for you.

Key Advantages of Perl:

  • Text Manipulation: Perl’s strength in text processing makes it invaluable for tasks like data extraction, reporting, and transformation.
  • Cross-Platform: Perl scripts can run on various platforms, ensuring flexibility and broad applicability.
  • CPAN: The Comprehensive Perl Archive Network (CPAN) provides a vast repository of modules and extensions, enhancing Perl’s functionality.

With its enterprise-grade stability, Rocky Linux serves as an ideal environment for Perl. The synergy between Perl’s adaptability and Rocky Linux’s reliability ensures optimal application performance.

In the subsequent sections, we’ll delve into the detailed steps to seamlessly install Perl on Rocky Linux 9 and the older stable release of Rocky Linux 8, ensuring you have the tools to harness Perl’s full potential.

Install Perl on Rocky Linux 9 or 8 via DNF

Step 1: Update your system

Start by updating the Rocky Linux system with this command:

sudo dnf upgrade --refresh

This command ensures the system’s packages are up-to-date before proceeding with the Perl installation.

Step 2: Install Perl and common packages on Rocky Linux 9 or 8

Next, install Perl along with common packages, such as development tools, by running:

sudo dnf install perl perl-App-cpanminus perl-devel

This command installs Perl, the cpanminus package manager for Perl modules, and the development tools package includes essential tools and libraries for compiling and building Perl modules.

Step 3: Verify Perl Installation on Rocky Linux 9 or 8

Check if Perl was installed successfully by running the following command:

perl -v

This should display the installed Perl version, along with some licensing information.

cpanm with Perl Library Modules on Rocky Linux 9 or 8

cpanm (short for CPAN Minus) is a lightweight and user-friendly package manager for Perl modules. It streamlines installing and managing Perl modules from the Comprehensive Perl Archive Network (CPAN). This section covers the basics of using cpanm to work with Perl library modules.

Install Perl Modules with cpanm on Rocky Linux

To install a Perl module using cpanm, run the following command:

sudo cpanm Module::Name

Replace Module::Name with the name of the desired module. For example, to install the popular Mojolicious web framework, execute:

sudo cpanm Mojolicious

Searching for Perl Modules

To search for specific Perl modules, visit the CPAN website at https://metacpan.org/ and use the search function to find the desired module.

Upgrade Perl Modules on Rocky Linux

To upgrade an installed Perl module to the latest version, use the same cpanm installation command:

sudo cpanm Module::Name

cpanm automatically detects the installed module and upgrades it to the latest version available on CPAN.

List Installed Perl Modules on Rocky Linux

To view a list of installed Perl modules, run:

cpanm -l

This command displays a list of installed modules along with their respective versions.

Remove Perl Modules on Rocky Linux

Although cpanm does not provide a direct way to uninstall Perl modules, you can remove them manually. First, locate the module’s installation path by running:

perldoc -l Module::Name

Then, remove the module’s files and directories using the rm command:

sudo rm -r /path/to/module/files

Be sure to replace /path/to/module/files with the actual path obtained from the perldoc command.

By understanding how to use cpanm, you can efficiently manage Perl library modules and customize your Perl environment to suit your specific needs.

Setup the Perl Environment on Rocky Linux 9 or 8

Adding Perl to the PATH variable

To execute Perl scripts from any directory, add the Perl binary to the system’s PATH variable with this command:

echo 'export PATH=$PATH:$(dirname $(which perl))' | sudo tee -a /etc/profile.d/perl.sh && source /etc/profile.d/perl.sh

This command creates a new file in the /etc/profile.d/ directory that appends the Perl binary’s path to the PATH variable and reloads the environment without needing to restart the terminal.

Basic Perl commands

CommandDescriptionExample
printOutput text or variables.print "Hello, World!\n";
myDeclare a scalar (single) variable.my $name = "Alice";
@Declare an array variable.my @colors = ("red", "blue", "green");
%Declare a hash variable.my %fruit_colors = ("apple" => "red", "banana" => "yellow");
if, elsif, elseConditional statements.if ($age > 18) { print "Adult\n"; } else { print "Minor\n"; }
for, foreachLooping constructs.foreach my $color (@colors) { print "$color\n"; }
while, untilLooping constructs with conditions.while ($counter < 10) { print "$counter\n"; $counter++; }
subDefine a subroutine (function).sub greet { my $name = shift; print "Hello, $name!\n"; }
open, closeFile handling operations.open my $file, "<", "input.txt"; close $file;
chompRemove trailing newline characters from a string.chomp(my $input = <STDIN>);

The table above summarizes basic Perl commands, their descriptions, and examples of usage.

Create and Configuring Perl Scripts Examples on Rocky Linux

Example 1:

To create a simple Perl script that prints “Hello, World!”, follow these steps:

  1. Create a new file named hello_world.pl.
  2. Open the file in a text editor and add the following shebang line at the top:
#!/usr/bin/perl
  1. Write the Perl code below the shebang line:
print "Hello, World!\n";
  1. Save the file and exit the text editor.
  2. Make the script executable by running:
chmod +x hello_world.pl
  1. Execute the script by running:
./hello_world.pl

Example 2:

To create a Perl script that calculates the sum of two numbers, follow these steps:

  1. Create a new file named sum_numbers.pl.
  2. Open the file in a text editor and add the following shebang line at the top:
#!/usr/bin/perl
  1. Write the Perl code below the shebang line:
my $num1 = 5;
my $num2 = 10;
my $sum = $num1 + $num2;

print "The sum of $num1 and $num2 is: $sum\n";
  1. Save the file and exit the text editor.
  2. Make the script executable by running:
chmod +x sum_numbers.pl
  1. Execute the script by running:
./sum_numbers.pl

Conclusion

This article provided a step-by-step guide to installing Perl on Rocky Linux, setting up the Perl environment, creating and configuring Perl scripts, and an overview of basic Perl commands. With this knowledge, you should be well-equipped to use Perl on your Rocky Linux system. Happy coding!

Leave a Comment


Your Mastodon Instance
Share to...