Date post: | 11-May-2015 |
Category: |
Technology |
Upload: | sreejith-sasidharan |
View: | 781 times |
Download: | 2 times |
Let’s Learn PerlAn introduction to Perl
Sreejith SNLP Process Analyst365Media Pvt. Ltd.
January 22, 2011
Sreejith S Let’s Learn Perl
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
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
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 thanbeautiful (tiny, elegant, minimal)
Perl was developed by Larry Wall.
Sreejith S Let’s Learn Perl
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
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.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.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
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
To install...
Perl Comes by Default on Linux, Cygwin, MacOSX
www.perl.com has rpm’s for Linuxwww.activestate.com has binaries for Windows
Latest Version is 5.10To check if Perl is working and the version number
perl -v or perl -version
Sreejith S Let’s Learn Perl
Sreejith S Let’s Learn Perl
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #
dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
Basic Syntax of Perl
Comments
begin with #dont try to use // or /* ... */
print is simple function to display/output something onmonitor/stdoute.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
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
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
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
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
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
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
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
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
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
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
Scalars
ListsArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsLists
ArraysHashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArrays
Hashes.
Sreejith S Let’s Learn Perl
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 timeDepending on the context will be treated as int, float etc.
There are 4 kinds of variables namely
ScalarsListsArraysHashes.
Sreejith S Let’s Learn Perl
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter or
remaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Scalars
Scalar meaning single value
Name of scalar variable must begin with $ symbol.
next character must a letter orremaining characters: letters, numbers, orname 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
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
Variable & Character Interpolation
Perl looks for variables inside strings and replaces them with theirvalue
$name = "Sree";
print "$name is my name\n";
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 doublequoted strings
All escape characters are recognized
print "Hello World!\n";
# It prints Hello World and then a return
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
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 "$first\n";
List variables can be assigned like this
($name,$age)=(’Raman’,20);
print "name=$name\n";
Sreejith S Let’s Learn Perl
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
Arrays
Array variables should have prefix ‘@‘
Arrays are used to store ordered values
Much more dynamic than C/C++
no declaration of size, typecan 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
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT
@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
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
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $
print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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=$last\n";
To prepend to the beginning of an array : UNSHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To remove the first element of the array : SHIFT@array=(3,4,5);
unshift @array,’2’;
print "array=@array\n";
To access the whole array, use the whole arrayprint @array;
To access one element of the array : use $print $array[0]; # prints : 1
Sreejith S Let’s Learn Perl
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
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
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
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
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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Hashes
Hash variables will have % as prefix
Key-Value pair
The contents of hash are called values and index is called key. thearray:
%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
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
Conditionals - IF
The if statement is similar to if in C language, except
flower brace is required even for single statementelse 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
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
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 $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specificcondition is met
$i = 0;
until ($i == 1000)
{
print $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
Control Structures - Until Loop
The until function evaluates an expression repeatedly until a specificcondition is met
$i = 0;
until ($i == 1000)
{
print $i\n;
$i++;
}
Sreejith S Let’s Learn Perl
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
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
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
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
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
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
Control Structures - ForEach Loop
Syntax 3:
foreach $i (a,b,c)
{
print uc $i;
}
Sreejith S Let’s Learn Perl
Control Structures - ForEach Loop
Syntax 3:
foreach $i (a,b,c)
{
print uc $i;
}
Sreejith S Let’s Learn Perl
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
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
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 $name\n";
Sreejith S Let’s Learn Perl
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 $name\n";
Sreejith S Let’s Learn Perl
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
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
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
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
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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 to new file
}
close($fh);
close($fh1);
Sreejith S Let’s Learn Perl
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Web Resource for Perl
Web resources for Perl
www.perl.comwww.perl.orgwww.perlmonks.org
Sreejith S Let’s Learn Perl
Web Resource for Perl
Web resources for Perl
www.perl.comwww.perl.orgwww.perlmonks.org
Sreejith S Let’s Learn Perl
Recommended Reading
Sreejith S Let’s Learn Perl
Questions
Sreejith S Let’s Learn Perl
Thank You
Sreejith S Let’s Learn Perl