1. Let’s Learn Perl
An introduction to Perl
Sreejith S
NLP Process Analyst
365Media Pvt. Ltd.
srssreejith@gmail.com
January 22, 2011
Sreejith S Let’s Learn Perl
2. Just a word about me !!
Working in Natural Language Processing (NLP), Machine Learning,
Data Mining
Passionate about Free and Open source :-)
Works for 365Media Pvt. Ltd. Coimbatore India.
Sreejith S Let’s Learn Perl
3. What’s Perl ??
Practical Extraction and Reporting Language
Sreejith S Let’s Learn Perl
4. What’s Perl ??
Practical Extraction and Reporting Language
Perl is a Portable Scripting Language
Sreejith S Let’s Learn Perl
5. What’s Perl ??
Practical Extraction and Reporting Language
Perl is a Portable Scripting Language
Fast and easy text processing capability
Sreejith S Let’s Learn Perl
6. What’s Perl ??
Practical Extraction and Reporting Language
Perl is a Portable Scripting Language
Fast and easy text processing capability
Fast and easy file handling capability
Sreejith S Let’s Learn Perl
7. What’s Perl ??
Practical Extraction and Reporting Language
Perl is a Portable Scripting Language
Fast and easy text processing capability
Fast and easy file handling capability
It aims to be practical (easy to use, efficient, complete) rather than
beautiful (tiny, elegant, minimal)
Sreejith S Let’s Learn Perl
8. What’s Perl ??
Practical Extraction and Reporting Language
Perl is a Portable Scripting Language
Fast and easy text processing capability
Fast and easy file handling capability
It aims to be practical (easy to use, efficient, complete) rather than
beautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
9. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
Sreejith S Let’s Learn Perl
10. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linux
Sreejith S Let’s Learn Perl
11. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linux
www.activestate.com has binaries for Windows
Sreejith S Let’s Learn Perl
12. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linux
www.activestate.com has binaries for Windows
Latest Version is 5.10
Sreejith S Let’s Learn Perl
13. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linux
www.activestate.com has binaries for Windows
Latest Version is 5.10
To check if Perl is working and the version number
Sreejith S Let’s Learn Perl
14. To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linux
www.activestate.com has binaries for Windows
Latest Version is 5.10
To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
17. Basic Syntax of Perl
Comments
begin with #
Sreejith S Let’s Learn Perl
18. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Sreejith S Let’s Learn Perl
19. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
Sreejith S Let’s Learn Perl
20. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
Sreejith S Let’s Learn Perl
21. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Sreejith S Let’s Learn Perl
22. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
Sreejith S Let’s Learn Perl
23. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
All Statements end with ; like C language
Sreejith S Let’s Learn Perl
24. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
All Statements end with ; like C language
Delimiter
Sreejith S Let’s Learn Perl
25. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
All Statements end with ; like C language
Delimiter
{} delimit blocks, loops, subroutines
Sreejith S Let’s Learn Perl
26. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
All Statements end with ; like C language
Delimiter
{} delimit blocks, loops, subroutines
No main() function
Sreejith S Let’s Learn Perl
27. Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
Print
print is simple function to display/output something on
monitor/stdout
e.g :- print “hello world“;
Statement terminator ;
All Statements end with ; like C language
Delimiter
{} delimit blocks, loops, subroutines
No main() function
Top-down programming approach
Sreejith S Let’s Learn Perl
28. Structure of a Perl Program
#! /usr/bin/perl -w
Sreejith S Let’s Learn Perl
29. Structure of a Perl Program
#! /usr/bin/perl -w
It tells where to find Perl
Sreejith S Let’s Learn Perl
30. Structure of a Perl Program
#! /usr/bin/perl -w
It tells where to find Perl
.pl extension is optional but is commonly used
Sreejith S Let’s Learn Perl
31. Structure of a Perl Program
#! /usr/bin/perl -w
It tells where to find Perl
.pl extension is optional but is commonly used
You can write perl programs without extension also.
Sreejith S Let’s Learn Perl
32. Structure of a Perl Program
#! /usr/bin/perl -w
It tells where to find Perl
.pl extension is optional but is commonly used
You can write perl programs without extension also.
-w switches on warning : not required but a really good idea
Sreejith S Let’s Learn Perl
33. Customary Hello World Program
#! /usr/bin/perl -w
print "Hello World!n";
Sreejith S Let’s Learn Perl
34. Customary Hello World Program
#! /usr/bin/perl -w
print "Hello World!n";
Save this as hello.pl
Sreejith S Let’s Learn Perl
35. Customary Hello World Program
#! /usr/bin/perl -w
print "Hello World!n";
Save this as hello.pl
To run the program
Sreejith S Let’s Learn Perl
36. Customary Hello World Program
#! /usr/bin/perl -w
print "Hello World!n";
Save this as hello.pl
To run the program
perl hello.pl or perl hello
Sreejith S Let’s Learn Perl
37. Variables
Variables are memory location to store information.
Sreejith S Let’s Learn Perl
38. Variables
Variables are memory location to store information.
Variables are type less
Sreejith S Let’s Learn Perl
39. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Sreejith S Let’s Learn Perl
40. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Sreejith S Let’s Learn Perl
41. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Sreejith S Let’s Learn Perl
42. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Sreejith S Let’s Learn Perl
43. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
Sreejith S Let’s Learn Perl
44. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Sreejith S Let’s Learn Perl
45. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
Sreejith S Let’s Learn Perl
46. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
Lists
Sreejith S Let’s Learn Perl
47. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
Lists
Arrays
Sreejith S Let’s Learn Perl
48. Variables
Variables are memory location to store information.
Variables are type less
There is no data type like int,char,float
Variables do not need to be declared
Every variable is a string
Variable type is decided at run time
Depending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
Lists
Arrays
Hashes.
Sreejith S Let’s Learn Perl
49. Scalars
Scalar meaning single value
Sreejith S Let’s Learn Perl
50. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
Sreejith S Let’s Learn Perl
51. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
Sreejith S Let’s Learn Perl
52. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
Sreejith S Let’s Learn Perl
53. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Sreejith S Let’s Learn Perl
54. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
Sreejith S Let’s Learn Perl
55. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Sreejith S Let’s Learn Perl
56. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
Sreejith S Let’s Learn Perl
57. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Sreejith S Let’s Learn Perl
58. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
Sreejith S Let’s Learn Perl
59. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”;
Sreejith S Let’s Learn Perl
60. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
Sreejith S Let’s Learn Perl
61. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30;
Sreejith S Let’s Learn Perl
62. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30; # in numerical context
Sreejith S Let’s Learn Perl
63. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30; # in numerical context
$value = 4.43;
Sreejith S Let’s Learn Perl
64. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30; # in numerical context
$value = 4.43; # in float context
Sreejith S Let’s Learn Perl
65. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30; # in numerical context
$value = 4.43; # in float context
$foo = Z;
Sreejith S Let’s Learn Perl
66. Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, or
name can be up to 255 characters long
Right
$foo, $a, $zebra1, $F87dr df3
Wrong
$24da, $hi&bye, $bar$foo
Examples
$name=”ram”; # in string context
$age=30; # in numerical context
$value = 4.43; # in float context
$foo = Z; # in char context
Sreejith S Let’s Learn Perl
67. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
Sreejith S Let’s Learn Perl
68. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
Sreejith S Let’s Learn Perl
69. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
This does not happen when you use single quotes
Sreejith S Let’s Learn Perl
70. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
This does not happen when you use single quotes
print ’$name is my name’; and it will print
Sreejith S Let’s Learn Perl
71. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
This does not happen when you use single quotes
print ’$name is my name’; and it will print
$name is my name
Sreejith S Let’s Learn Perl
72. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
This does not happen when you use single quotes
print ’$name is my name’; and it will print
$name is my name
List of character escapes that are recognized when using double
quoted strings
All escape characters are recognized
Sreejith S Let’s Learn Perl
73. Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with their
value
$name = "Sree";
print "$name is my namen";
This does not happen when you use single quotes
print ’$name is my name’; and it will print
$name is my name
List of character escapes that are recognized when using double
quoted strings
All escape characters are recognized
print "Hello World!n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
74. Lists
List variables are denoted by symbol ‘()‘
Sreejith S Let’s Learn Perl
75. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
Sreejith S Let’s Learn Perl
76. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
(a,b,c) or ($name,$age,$sex)
Sreejith S Let’s Learn Perl
77. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
(a,b,c) or ($name,$age,$sex)
The index are specified inside a square bracket ‘[]‘
Sreejith S Let’s Learn Perl
78. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
(a,b,c) or ($name,$age,$sex)
The index are specified inside a square bracket ‘[]‘
$first=(a,b,c)[0];
print "$firstn";
Sreejith S Let’s Learn Perl
79. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
(a,b,c) or ($name,$age,$sex)
The index are specified inside a square bracket ‘[]‘
$first=(a,b,c)[0];
print "$firstn";
List variables can be assigned like this
Sreejith S Let’s Learn Perl
80. Lists
List variables are denoted by symbol ‘()‘
List is just a list of values may be constants, scalars etc
(a,b,c) or ($name,$age,$sex)
The index are specified inside a square bracket ‘[]‘
$first=(a,b,c)[0];
print "$firstn";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$namen";
Sreejith S Let’s Learn Perl
81. Arrays
Array variables should have prefix ‘@‘
Sreejith S Let’s Learn Perl
82. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Sreejith S Let’s Learn Perl
83. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
Sreejith S Let’s Learn Perl
84. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
Sreejith S Let’s Learn Perl
85. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Sreejith S Let’s Learn Perl
86. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Array Indexes start at 0
Sreejith S Let’s Learn Perl
87. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Array Indexes start at 0
@array=(1,2,3);
To access the whole array, use the whole array
Sreejith S Let’s Learn Perl
88. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Array Indexes start at 0
@array=(1,2,3);
To access the whole array, use the whole array
print @array;
Sreejith S Let’s Learn Perl
89. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Array Indexes start at 0
@array=(1,2,3);
To access the whole array, use the whole array
print @array;
To access one element of the array : use $
Sreejith S Let’s Learn Perl
90. Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, type
can hold any kind of value, and multiple kinds of values
Array Indexes start at 0
@array=(1,2,3);
To access the whole array, use the whole array
print @array;
To access one element of the array : use $
print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
91. Operations on Arrays
To append to the end of an array : PUSH
Sreejith S Let’s Learn Perl
92. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
Sreejith S Let’s Learn Perl
93. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
Sreejith S Let’s Learn Perl
94. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
Sreejith S Let’s Learn Perl
95. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
Sreejith S Let’s Learn Perl
96. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
97. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
Sreejith S Let’s Learn Perl
98. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
99. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
100. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
101. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
102. Operations on Arrays
To append to the end of an array : PUSH
@array=(1,2,3);
push @array,’4’;
print "@array n";
To remove the last element of the array : POP
@array=(1,2,3);
$last=pop @array;
print "last=$lastn";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@arrayn";
Sreejith S Let’s Learn Perl
103. Operations on Arrays Contind...
$#array is a special variable containing index of last element.
Sreejith S Let’s Learn Perl
104. Operations on Arrays Contind...
$#array is a special variable containing index of last element.
print $#array; # it will print 2
Sreejith S Let’s Learn Perl
105. Operations on Arrays Contind...
$#array is a special variable containing index of last element.
print $#array; # it will print 2
Another way to find the number of elements in the array:
Sreejith S Let’s Learn Perl
106. Operations on Arrays Contind...
$#array is a special variable containing index of last element.
print $#array; # it will print 2
Another way to find the number of elements in the array:
$array_size = @array;
scalar(@array) is a function to return the size of array
Sreejith S Let’s Learn Perl
107. Operations on Arrays Contind...
$#array is a special variable containing index of last element.
print $#array; # it will print 2
Another way to find the number of elements in the array:
$array_size = @array;
scalar(@array) is a function to return the size of array
Sreejith S Let’s Learn Perl
108. Hashes
Hash variables will have % as prefix
Sreejith S Let’s Learn Perl
109. Hashes
Hash variables will have % as prefix
Key-Value pair
Sreejith S Let’s Learn Perl
110. Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. the
array:
Sreejith S Let’s Learn Perl
111. Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. the
array:
%fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
’grape’ =>’black’);
print "colour of apple =$fruits{apple}n";
Sreejith S Let’s Learn Perl
112. Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. the
array:
%fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
’grape’ =>’black’);
print "colour of apple =$fruits{apple}n";
Other way of populating a hash
Sreejith S Let’s Learn Perl
113. Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. the
array:
%fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
’grape’ =>’black’);
print "colour of apple =$fruits{apple}n";
Other way of populating a hash
%fruits =(’apple’,’red’,’banana’,’yellow’,
’grape’,’black’);
print "colour of banana =$fruits{banana}n";
Sreejith S Let’s Learn Perl
114. Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. the
array:
%fruits= ( ’apple’=>’red’,’banana’=>’yellow’,
’grape’ =>’black’);
print "colour of apple =$fruits{apple}n";
Other way of populating a hash
%fruits =(’apple’,’red’,’banana’,’yellow’,
’grape’,’black’);
print "colour of banana =$fruits{banana}n";
Note the {} instead of [ ] as in the case of array;
Sreejith S Let’s Learn Perl
115. Conditionals - IF
The if statement is similar to if in C language, except
Sreejith S Let’s Learn Perl
116. Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statement
else if is noted by elsif (note missing e)
Sreejith S Let’s Learn Perl
117. Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statement
else if is noted by elsif (note missing e)
The syntax of if statement is
Sreejith S Let’s Learn Perl
118. Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statement
else if is noted by elsif (note missing e)
The syntax of if statement is
if (condition) {
}
elsif (condition){
}
else {
}
Sreejith S Let’s Learn Perl
119. Control Structures - Loops
Perl supports four main loop types
Sreejith S Let’s Learn Perl
120. Control Structures - Loops
Perl supports four main loop types
while, until, for, and foreach
Sreejith S Let’s Learn Perl
121. Control Structures - Loops
Perl supports four main loop types
while, until, for, and foreach
while loop is used to iterate and has syntax similar to C.
Sreejith S Let’s Learn Perl
122. Control Structures - Loops
Perl supports four main loop types
while, until, for, and foreach
while loop is used to iterate and has syntax similar to C.
$i = 0;
while ( $i <= 1000 )
{
print $in;
$i++;
}
Sreejith S Let’s Learn Perl
123. Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specific
condition is met
Sreejith S Let’s Learn Perl
124. Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specific
condition is met
$i = 0;
until ($i == 1000)
{
print $in;
$i++;
}
Sreejith S Let’s Learn Perl
125. Control Structures - For Loop
For loop syntax is similar to c.
Sreejith S Let’s Learn Perl
126. Control Structures - For Loop
For loop syntax is similar to c.
There are variations
Sreejith S Let’s Learn Perl
127. Control Structures - For Loop
For loop syntax is similar to c.
There are variations
Syntax 1:
Sreejith S Let’s Learn Perl
128. Control Structures - For Loop
For loop syntax is similar to c.
There are variations
Syntax 1:
for ( $i = 0; $i <= 1000; $i=$i+2 )
{
print $i;
}
Sreejith S Let’s Learn Perl
129. Control Structures - For Loop
For loop syntax is similar to c.
There are variations
Syntax 1:
for ( $i = 0; $i <= 1000; $i=$i+2 )
{
print $i;
}
Syntax 2:
Sreejith S Let’s Learn Perl
130. Control Structures - For Loop
For loop syntax is similar to c.
There are variations
Syntax 1:
for ( $i = 0; $i <= 1000; $i=$i+2 )
{
print $i;
}
Syntax 2:
for $i(0..1000)
{
print $i;
}
Sreejith S Let’s Learn Perl
134. Default scalar variable
$ is called default variable
It will be used if no other variable is specified
foreach (a,b,c){
print uc ;
}
Sreejith S Let’s Learn Perl
135. Accepting input
Keyboard inputs can be accepted using STDIN
Sreejith S Let’s Learn Perl
136. Accepting input
Keyboard inputs can be accepted using STDIN
print "enter your name ";
$name=<STDIN>;
chomp $name;
# chomp function is similar to fflush in C.
# It removes trailing newline if any
print "Welcome $namen";
Sreejith S Let’s Learn Perl
137. Subroutines
Subroutines can be defined using sub keyword
Sreejith S Let’s Learn Perl
138. Subroutines
Subroutines can be defined using sub keyword
’@ ’ default array
$v1=10;$v2=20;
add($v1,$v2);
sub add {
($a,$b)=@_;
print $a+$b;
}
Sreejith S Let’s Learn Perl
139. Scope of variables
By default all variables are global
Sreejith S Let’s Learn Perl
140. Scope of variables
By default all variables are global
You can limit scope to a block/sub by using my.
$v1=10; $v2=30; #v1,v2 global
my $i = 10;
Sreejith S Let’s Learn Perl
141. File handling
File handling can be done very easily
Sreejith S Let’s Learn Perl
142. File handling
File handling can be done very easily
To open a file in read mode
Sreejith S Let’s Learn Perl
143. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
Sreejith S Let’s Learn Perl
144. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
File reading line by line can be done like
Sreejith S Let’s Learn Perl
145. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
File reading line by line can be done like
$line=<$fh>;
Sreejith S Let’s Learn Perl
146. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
File reading line by line can be done like
$line=<$fh>;
File writing can be done using print and filehandle like this
print $fh "hello";
Sreejith S Let’s Learn Perl
147. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
File reading line by line can be done like
$line=<$fh>;
File writing can be done using print and filehandle like this
print $fh "hello";
Example
Sreejith S Let’s Learn Perl
148. File handling
File handling can be done very easily
To open a file in read mode
open( $fh, < , data.txt);
File reading line by line can be done like
$line=<$fh>;
File writing can be done using print and filehandle like this
print $fh "hello";
Example
open ( $fh, "<", "data.txt" ); #open file read only
open ($fh1,">","udata.txt"); #Open file write mode
while ( $line = <$fh>; ) { #read line by
print "line=$line"; #display content on screen
print $fh1 uc($line); #write upper cased content t
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
149. Some Common String Functions
There are many built in string functions
Sreejith S Let’s Learn Perl
150. Some Common String Functions
There are many built in string functions
Convert to upper case
Sreejith S Let’s Learn Perl
151. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Sreejith S Let’s Learn Perl
152. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
Sreejith S Let’s Learn Perl
153. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Sreejith S Let’s Learn Perl
154. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
Sreejith S Let’s Learn Perl
155. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
$name = lc($name);
Sreejith S Let’s Learn Perl
156. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
$name = lc($name);
Convert only the first char to lower case
Sreejith S Let’s Learn Perl
157. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
$name = lc($name);
Convert only the first char to lower case
$name = lcfirst($name);
Sreejith S Let’s Learn Perl
158. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
$name = lc($name);
Convert only the first char to lower case
$name = lcfirst($name);
Concatenation: ’.’ similar to strcat
Sreejith S Let’s Learn Perl
159. Some Common String Functions
There are many built in string functions
Convert to upper case
$name = uc($name);
Convert only the first char to upper case
$name = ucfirst($name);
Convert to lower case
$name = lc($name);
Convert only the first char to lower case
$name = lcfirst($name);
Concatenation: ’.’ similar to strcat
$first_name = Larry;
$last_name = Wall;
$full_name = $first_name . . $last_name;
Sreejith S Let’s Learn Perl
160. Some Common Functions
There are many built in functions
Sreejith S Let’s Learn Perl
161. Some Common Functions
There are many built in functions
Strings to Arrays : split
Sreejith S Let’s Learn Perl
162. Some Common Functions
There are many built in functions
Strings to Arrays : split
@array = split( /;/, "Larry;Curly;Moe" );
@array= ("Larry", "Curly", "Moe");
Sreejith S Let’s Learn Perl
163. Some Common Functions
There are many built in functions
Strings to Arrays : split
@array = split( /;/, "Larry;Curly;Moe" );
@array= ("Larry", "Curly", "Moe");
Split into characters
Sreejith S Let’s Learn Perl
164. Some Common Functions
There are many built in functions
Strings to Arrays : split
@array = split( /;/, "Larry;Curly;Moe" );
@array= ("Larry", "Curly", "Moe");
Split into characters
@stooge = split( //, "curly" );
# array @stooge has 5 elements: c, u, r, l, y
Sreejith S Let’s Learn Perl
165. Some Common Functions
There are many built in functions
Strings to Arrays : split
@array = split( /;/, "Larry;Curly;Moe" );
@array= ("Larry", "Curly", "Moe");
Split into characters
@stooge = split( //, "curly" );
# array @stooge has 5 elements: c, u, r, l, y
Join with any character you want
Sreejith S Let’s Learn Perl
166. Some Common Functions
There are many built in functions
Strings to Arrays : split
@array = split( /;/, "Larry;Curly;Moe" );
@array= ("Larry", "Curly", "Moe");
Split into characters
@stooge = split( //, "curly" );
# array @stooge has 5 elements: c, u, r, l, y
Join with any character you want
@array = 10, 20, 30, 40);
$string = join(->, @array);
# string = 10->20->30->40
Sreejith S Let’s Learn Perl
167. Web Resource for Perl
Web resources for Perl
Sreejith S Let’s Learn Perl
168. Web Resource for Perl
Web resources for Perl
www.perl.com
www.perl.org
www.perlmonks.org
Sreejith S Let’s Learn Perl