Introduction to Perl Scripting and installation of perl with perlbrew on centos 7

Introduction to Perl Scripting

Perl Language

Perl is used on millions of computers, and it's one of the fastest-growing programming languages available. It is a stable, cross-platform programming language.
Perl is not a difficult language to learn. It's a language that tries to shape itself around the way humans think about problems and provides nothing contrary to their expectations.

Features

Perl is portable. Perl program can run on any operating system that has Perl installed.
Perl's mod_perl allows the Apache web server to embed a Perl interpreter.
Perl's DBI package makes web-database integration easy.
It can integrate with almost all modern databases, work with HTML, XML and other markup languages.
It supports Unicode.
CPAN: Comprehensive Perl Archive Network, your one-stop archive for everything related to Perl. It has source codes, documentation, Perl examples, extensions etc

In order to download Perl go to its official website http://www.perl.org/get.html
Select the respective distribution from the above link and download them in any archive format
Download and installation of Perl on Centos 7 (Linux)
By default centos 7 comes with the perl installed with the default version

\[root@linuxhelp ~\]# perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for x86\_64-linux-thread-multi
(with 33 registered patches, see perl -V for more detail)
Copyright 1987-2012, 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.

We definitely want to upgrade our version, being able to use up-to-date Perl modules in our code. Fortunately, there is a tool for maintaining Perl environments and keeping our CentOS version of Perl isolated. It is called perlbrew.

\[root@linuxhelp ~\]# rpm -qa | grep gcc
libgcc-4.8.5-16.el7\_4.2.x86\_64
\[root@linuxhelp ~\]# yum install gcc -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 \* base: mirror.nbrc.ac.in
 \* extras: mirror.nbrc.ac.in
 \* updates: mirror.nbrc.ac.in
Resolving Dependencies
--> Running transaction check
---> Package gcc.x86\_64 0:4.8.5-28.el7\_5.1 will be installed
--> Processing Dependency: libgomp = 4.8.5-28.el7\_5.1 for package: gcc-4.8.5-28.el7\_5.1.x86\_64.
.
.
.
.
Dependency Updated:
  libgcc.x86\_64 0:4.8.5-28.el7\_5.1                                                                                   libgomp.x86\_64 0:4.8.5-28.el7\_5.1
Complete!

And also check whether bzip is installed in your machine or not by,

\[root@linuxhelp ~\]# rpm -qa | grep bzip2
bzip2-1.0.6-13.el7.x86\_64
bzip2-libs-1.0.6-13.el7.x86\_64

Finally install Patch

\[root@linuxhelp ~\]# yum install patch -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 \* base: mirror.xeonbd.com
 \* extras: mirrors.fibergrid.in
 \* updates: mirrors.fibergrid.in
Resolving Dependencies
--> Running transaction check
---> Package patch.x86\_64 0:2.7.1-10.el7\_5 will be installed
--> Finished Dependency Resolution
.
.
.
Running transaction
  Installing : patch-2.7.1-10.el7\_5.x86\_64                                                                                                                                                                                          1/1
  Verifying  : patch-2.7.1-10.el7\_5.x86\_64                                                                                                                                                                                          1/1

Installed:
  patch.x86\_64 0:2.7.1-10.el7\_5

Complete!

you can install Perlbrew. Download the installation script to your server by

\[root@linuxhelp ~\]# curl -L https://install.perlbrew.pl -o install.perlbrew.pl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   170  100   170    0     0    158      0  0:00:01  0:00:01 --:--:--   158
100  1548  100  1548    0     0    854      0  0:00:01  0:00:01 --:--:--   854

To check the contents of the script before running it

\[root@linuxhelp ~\]# ls -l
total 12
-rw-------. 1 root root 1757 Mar 22 09:06 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Desktop
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Documents
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Downloads
-rw-r--r--. 1 root root 1805 Mar 22 09:16 initial-setup-ks.cfg
-rw-r--r--  1 root root 1548 Jun 27 14:39 install.perlbrew.
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Music
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Pictures
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Public
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Templates
drwxr-xr-x. 2 root root    6 Mar 22 09:20 Videos

Rename it

\[root@linuxhelp ~\]# mv install.perlbrew. install.perlbrew.pl

Install perlbrew by pipelining it with bash

\[root@linuxhelp ~\]# cat install.perlbrew.pl | bash
## Download the latest perlbrew
## Installing perlbrew
Using Perl 
perlbrew is installed: ~/perl5/perlbrew/bin/perlbrew
perlbrew root (~/perl5/perlbrew) is initialized.
## Installing patchperl
## Done.

you should edit your .bash_profile file so it automatically sets some important Perlbrew environment variables.
Perlbrew makes this easy by including the needed code in another configuration file which you can include in your .bash_profile. Run the following,

\[root@linuxhelp ~\]# source ~/perl5/perlbrew/etc/bashrc

Save and exit the editor and finally check that these environment variables have been set by running the env command and filtering the results with grep

\[root@linuxhelp ~\]# env | grep PERL
PERLBREW\_SHELLRC\_VERSION=0.84
PERLBREW\_ROOT=/root/perl5/perlbrew
PERLBREW\_HOME=/root/.perlbrew

These above environment variables tell Perlbrew where important directories are located. Next inorder to know the full path of perlbrew type which perlbrew should now identify the full path to the perlbrew command.

\[root@linuxhelp ~\]# which perlbrew
/root/perl5/perlbrew/bin/perlbrew

Next to list the types of perl available run the following

\[root@linuxhelp ~\]# perlbrew available
   perl-5.29.0
   perl-5.28.0
   perl-5.26.2
   perl-5.26.1
   perl-5.26.0
   perl-5.24.4
   perl-5.24.3
   perl-5.24.2
   perl-5.24.1
.
.
.
perl-5.8.8
perl-5.8.7
perl-5.8.6
 perl-5.8.5
 perl-5.8.4
perl5.005\_04

Now install Perl latest version by

\[root@linuxhelp ~\]# perlbrew install perl-5.28.0
Fetching perl 5.28.0 as /root/perl5/perlbrew/dists/perl-5.28.0.tar.gz
Download http://www.cpan.org/authors/id/X/XS/XSAWYERX/perl-5.28.0.tar.gz to /root/perl5/perlbrew/dists/perl-5.28.0.tar.gz
Installing /root/perl5/perlbrew/build/perl-5.28.0/perl-5.28.0 into ~/perl5/perlbrew/perls/perl-5.28.0
This could take a while. You can run the following command on another shell to track the status:
  tail -f ~/perl5/perlbrew/build.perl-5.28.0.log
perl-5.28.0 is successfully installed.

Now inorder to use the installed version

\[root@linuxhelp ~\]# perlbrew use perl-5.28.0

If you want a version of Perl to be your default each time you log in, run this command

\[root@linuxhelp ~\]# perlbrew switch perl-5.28.0

Finally verify the installed version

\[root@linuxhelp ~\]# perl –V
This is perl 5, version 28, subversion 0 (v5.28.0) built for x86\_64-linux
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2018, 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.

Perlbrew installs just the core Perl code. To see which modules comprise the core for a particular version of Perl, execute this command

\[root@linuxhelp ~\]# corelist -v 5.28.0
The following modules were in perl 5.28.0 CORE
Amiga::ARexx                                 0.04
Amiga::Exec                                  0.02
AnyDBM\_File                                  1.01
App::Cpan                                    1.67
App::Prove                                   3.42
App::Prove::State                            3.42
App::Prove::State::Result                    3.42
App::Prove::State::Result::Test              3.42
Archive::Tar                                     2.28
Archive::Tar::Constant                       2.28
Archive::Tar::File                           2.28
Attribute::Handlers                          1.01
AutoLoader                                   5.74
AutoSplit                                    1.06
.
.
.
threads::shared                              1.58
unicore::Name
utf8                                         1.21
vars                                         1.04
version                                      0.9923
version::regex                               0.9923
vmsish                                       1.04
warnings                                     1.42
warnings::register                           1.04
Testing Your Perl Installation by Installing Modules from CPAN
It has vast array of public code modules that extend the core language and these are scored in Comprehensive Perl Archive Network (CPAN)
App::cpanminus is a Perl module that can explore the CPAN repository and quickly download modules
 \[root@linuxhelp ~\]# curl -L https://cpanmin.us | perl - App::cpanminus
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
	Dload  Upload   Total   Spent    Left  Speed
100  295k  100  295k    0     0   138k      0  0:00:02  0:00:02 --:--:--  139k
--> Working on App::cpanminus
Fetching http://www.cpan.org/authors/id/M/MI/MIYAGAWA/App-cpanminus-1.7044.tar.gz ... OK
Configuring App-cpanminus-1.7044 ... OK
Building and testing App-cpanminus-1.7044 ... OK
Successfully installed App-cpanminus-1.7044
1 distribution installed

Verify that the cpanminus module is now present [root@linuxhelp ~]# perlbrew list-modules App::cpanminus
Perl
You can now use the command-line utility cpanm to install additional modules and their dependencies here Email::Simple module is being installed if incase you were writing some Perl code to send email messages.

\[root@linuxhelp ~\]# cpanm Email::Simple
--> Working on Email::Simple
Fetching http://www.cpan.org/authors/id/R/RJ/RJBS/Email-Simple-2.216.tar.gz ... OK
Configuring Email-Simple-2.216 ... OK
==> Found dependencies: Email::Date::Format
--> Working on Email::Date::Format
Fetching http://www.cpan.org/authors/id/R/RJ/RJBS/Email-Date-Format-1.005.tar.gz ... OK
Configuring Email-Date-Format-1.005 ... OK
Building and testing Email-Date-Format-1.005 ... OK
Successfully installed Email-Date-Format-1.005
Building and testing Email-Simple-2.216 ... OK
Successfully installed Email-Simple-2.216
2 distributions installed
\[root@linuxhelp ~\]# perlbrew list-modules
App::cpanminus
Email::Date::Format
Email::Simple
Perl

Again list and see the modules

\[root@linuxhelp ~\]# perlbrew list-modules
App::cpanminus
Email::Date::Format
Email::Simple
Perl
The Email::Simple module requires the Email:Date::Format module. which cpanm program installed the dependency for us.
FAQ
Q
What are scalar data types in perl?
A
Scalars are simple variables. They are preceded by a dollar sign ($). A scalar is either a number, a string, or a reference. A reference is actually an address of a variable, which we will see in the upcoming chapters.
Q
What are data types that perl supports?
A
Perl has three basic data types − scalars, arrays of scalars, and hashes of scalars, also known as associative arrays.
Q
What is a perl identifier?
A
A Perl identifier is a name used to identify a variable, function, class, module, or another object. A Perl variable name starts with either $, @ or % followed by zero or more letters, underscores, and digits (0 to 9).
Q
What are the features of Perl programming?
A
Perl used to be the most popular web programming language due to its text manipulation capabilities and rapid development cycle.

Perl is widely known as " the duct-tape of the Internet".

Perl can handle encrypted Web data, including e-commerce transactions.

Perl can be embedded into web servers to speed up processing by as much as 2000%.

Perl's mod_perl allows the Apache web server to embed a Perl interpreter.

Perl's DBI package makes web-database integration easy.
Q
Why do you write a program in Perl?
A
Easy to use and fast execution since Perl script undergoes only two phases like compile phase and run phase