28-05-2020
Perl Basics for Scripting
www.maven-silicon.com 1
Maven Silicon Confidential
All the presentations, books, documents [hard copies and soft copies] and
projects that you are using and developing as part of the training course are the
proprietary work of Maven Silicon and is fully protected under copyright and
trade secret laws. You may not view, use, disclose, copy, or distribute the
materials or any information except pursuant to a valid written license from
Maven Silicon
www.maven-silicon.com 2
1
28-05-2020
What is Perl?
Perl is short for “Practical Extraction and
Report Language”. High level
language
Larry Wall created Perl in the mid-1980s.
Perl is a high level, interpreted and dynamic Perl
Interpreter
computer programming language.
It is used for a variety of purposes including
Linux system administration, network Low level
language
programming, web development, etc.
www.maven-silicon.com 3
Why Perl?
Perl is extremely portable.It can run on any operating system that has Perl
interpreter installed, so it is platform independent.
Perl is free to use.
It is object oriented.
This is an interpreted language with fast execution time as there is no need to
compile a Perl script.
www.maven-silicon.com 4
2
28-05-2020
Where is Perl used?
Webpage Development
Perl is used to automate many tasks Webpage Scripting &
in automations. development Automation
Perl used in System Administration.
Perl used in Networking.
PERL
System Networking
Administration
www.maven-silicon.com 5
Perl Datatypes
Scalars: A scalar variable holds a single
value. Scalars
$scalar_name
Arrays: An array variable holds multiple Arrays
values in the form of a list using index.
@array_name Hashes
Hashes:A hash variable holds multiple
values in the form of a list using hash keys.
%hash_name
www.maven-silicon.com 6
3
28-05-2020
Scalars
Scalar
The scalar name should start
with either an alphabet or
underscore and can be
alphanumeric.
Valid legal names
Strings Numbers
$var
$var32
$var_num Single Double
quoted quoted
Integers Floating Octal Hexa Binary
point decimal
7
www.maven-silicon.com
Strings
A string can have as many characters which depends on the system memory.
Single quotes string literals
$s = ‘It’s a Perl session’;
Double quotes string literals
$v = “It’s a Perl session”;
A double quoted string only can perform variable expansion and expand escape
characters.
$var
PERL
www.maven-silicon.com 8
4
28-05-2020
Numbers
Integers
It’s a whole number which can be signed or unsigned.
Examples: 0; -23; 142
Floating point numbers
It’s a real number.
Examples:3.14; -3.12
Octal
Its preceded by 0.
Examples:037
Hexadecimal
Its preceded by 0x.
Examples:0x4a
Binary
Its preceded by 0b.
Examples:0b110
www.maven-silicon.com 9
Arrays
An Array is a special type of variable which stores data in the form of a list.
Each element can be accessed using the index number which will be unique
for each and every element. The index starts from 0.
An array can store numbers, strings, floating values.
1 2 1 a b
Index 0 1 2 3
www.maven-silicon.com 10
5
28-05-2020
Hash
A hash is a special type of variable which can hold a list of scalars.
A hash uses strings to access the values unlike index in an array. It is called as hash
keys.
An hash can store numbers, strings, floating values.
key1 1
key2 2
key3 a
key4 b
www.maven-silicon.com 11
Operators
Arithmetical operators
+,-,*,/,**,%,++,--
Arithmetical Assignment Logical Relational
Assignment operators
+=,-=,*=,/=,**=,%=
Relational operators
Used to compare numbers & strings.
==,eq,!=,ne,>,gt,<,lt,>=,ge,<=,le
www.maven-silicon.com 12
6
28-05-2020
Continued…
Logical operators
||,or,&&,and
String repetition operator
x
$string x 4
www.maven-silicon.com 13
Operators precedence
Operators Associativity
++,-- Null
Highest
** Right
*, /,% Left
+,- Left
>,<.,<=,>=,lt,gt,le,ge Null
==,!=,eq,ne Null
&& Left
|| Left
+=,-=,*=,/= Right
not Left
and Left
or Left Lowest
www.maven-silicon.com 14
7
28-05-2020
Perl conditional branches
Conditional statements are
those which gets executed
under a specific condition.
if
if else
if elsif
www.maven-silicon.com 15
Perl controlled loops
for loop
The for loop will execute till
the condition is satisfied.
foreach loop
The foreach loop iterates over
a list of elements and is auto-
incremented without any
condition check.
while loop
The while loop will execute
the statements till the condition
is true.
www.maven-silicon.com 16
8
28-05-2020
Perl Subroutines
Subroutines are similar to functions.
Perl also support built-in functions.
www.maven-silicon.com 17
Perl built-in functions
print : It’s a function which prints its
arguments to its STDOUT.
chop: It’s a function which removes
any last character from a string.
chomp: It’s a function which removes
only new line character from end of a
scalar.
open: This function is used in File
read & write operations.
www.maven-silicon.com 18
9
28-05-2020
Continued…
system: This executes a given Unix
command as a separate process.
reverse: Reverses the given array
elements.
www.maven-silicon.com 19
Methods of arrays
www.maven-silicon.com 20
10
28-05-2020
“pop” in Arrays
www.maven-silicon.com 21
“push” in Arrays
www.maven-silicon.com 22
11
28-05-2020
“Shift” in array
www.maven-silicon.com 23
“Unshift” in Arrays
www.maven-silicon.com 24
12
28-05-2020
“Slice” in array
www.maven-silicon.com 25
“Splice” in array
www.maven-silicon.com 26
13
28-05-2020
Perl Special variables
Special variables are those who have a pre-defined
meaning.
It can be a Scalar, an Array or a Hash.
These variables use a punctuation character at end.
Scalar
$_ : It’s a default scalar variable which contains
the default input & pattern searching space.
$1,$2 : It holds the values of the pattern
matched.
Array
@_ : It’s a default array variable. Used in sub-
routines while passing parameters to the sub-
routines.
@ARGV : Stores the command line arguments
passed.
www.maven-silicon.com 27
File operation in Perl
File handles are used to read from or write to an
external file.
A File handle is a string written in uppercase
letter without quotes.
File write
open(FILEH,”>file.txt”); #write mode
open(FILEH,”>>file.txt”); #append mode File
FileWrite
Read
File to
Script
Filee to e
File read
Output
open(FILEH,”file.txt”); #Read mode console
file.txt
file.txt
www.maven-silicon.com 28
14
28-05-2020
Continued…
www.maven-silicon.com 29
Continued…
www.maven-silicon.com 30
15
28-05-2020
File read operation
www.maven-silicon.com 31
@ARGV in FILE operation
@ARGV
Command line arguments
are automatically copied
into this array.
www.maven-silicon.com 32
16
28-05-2020
Perl regular expressions
Regular expressions are used in
pattern matching within a
statement or group of
statements.
Operators used for the same is
denoted as =~ . Its called as
pattern binding operator.
A pattern is represented within
/pattern/ .
www.maven-silicon.com 33
Special characters in Regular expressions
\w One letter, digit or underscore
\w+ One or more letters, digits or underscores
\s White space character, : space, tab or newline
\d Matches a digit
\d{n} Matches exactly n times
\d{m,n} Matches at least m times and at most n times
\d+ Matches one or more digits
( ) Pattern Memory
*, +, {} Number of occurrences
^ , $ , \b Pattern anchors
| Alternatives
[a-z] Only alphabets
[0-9] Only digits
www.maven-silicon.com 34
17
28-05-2020
Examples on Pattern matching with special characters
www.maven-silicon.com 35
Examples on Pattern matching with special characters
www.maven-silicon.com 36
18
28-05-2020
www.maven-silicon.com 37
Perl Modules & Packages
Perl Modules are collections of reusable Spreadsheet::WriteExcel
codes called packages.
Perl Modules are defined in a file with
.pm as the extension.
These modules can be loaded in Perl
Spreadsheet::ParseExcel
scripts to make use of the sub-routines.
CPAN: Comprehensive Perl Archive
Network
www.cpan.org
www.maven-silicon.com 38
19
28-05-2020
Write Excel
For e.g
use Spreadsheet::WriteExcel ;
Module name
my $book = Spreadsheet::WriteExcel->new('report.xls');
$sheet = $book->add_worksheet( );
$format = $book->add_format( );
$format->set_bold();
$format->set_color('red');
$format->set_align('center');
$sheet->write_string ($row,$col,$name,$format);
$sheet->write($row,$col_next,$b,$format);
39
References
https://www.tutorialspoint.com/perl/
https://www.guru99.com/perl-tutorials.html
www.maven-silicon.com 40
20
28-05-2020
Thank you
www.maven-silicon.com 41
21