335-PERL Reference Book
335-PERL Reference Book
www.maven-silicon.com 1
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?
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.
It is object oriented.
www.maven-silicon.com 4
2
28-05-2020
Webpage Development
Perl is used to automate many tasks Webpage Scripting &
in automations. development Automation
System Networking
Administration
www.maven-silicon.com 5
Perl Datatypes
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
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
key1 1
key2 2
key3 a
key4 b
www.maven-silicon.com 11
Operators
Arithmetical operators
+,-,*,/,**,%,++,--
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
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
www.maven-silicon.com 15
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
www.maven-silicon.com 17
www.maven-silicon.com 18
9
28-05-2020
Continued…
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
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
www.maven-silicon.com 31
@ARGV
Command line arguments
are automatically copied
into this array.
www.maven-silicon.com 32
16
28-05-2020
www.maven-silicon.com 33
www.maven-silicon.com 34
17
28-05-2020
www.maven-silicon.com 35
www.maven-silicon.com 36
18
28-05-2020
www.maven-silicon.com 37
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