+ All Categories

Perl

Date post: 11-May-2015
Category:
Upload: sreejith-sasidharan
View: 781 times
Download: 2 times
Share this document with a friend
Description:
Introfuction to Perl
Popular Tags:
171
Let’s Learn Perl An introduction to Perl Sreejith S NLP Process Analyst 365Media Pvt. Ltd. [email protected] January 22, 2011 Sreejith S Let’s Learn Perl
Transcript
Page 1: Perl

Let’s Learn PerlAn introduction to Perl

Sreejith SNLP Process Analyst365Media Pvt. Ltd.

[email protected]

January 22, 2011

Sreejith S Let’s Learn Perl

Page 2: 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

Page 3: 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

Page 4: 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

Page 5: 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

Page 6: 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

Page 7: 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

Page 8: 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

Page 9: 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

Page 10: 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

Page 11: 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

Page 12: 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

Page 13: 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

Page 14: 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

Page 15: Perl

Sreejith S Let’s Learn Perl

Page 16: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 17: Perl

Basic Syntax of Perl

Comments

begin with #

dont try to use // or /* ... */

Print

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

Page 18: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 19: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 20: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 21: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 22: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 23: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 24: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 25: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 26: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 27: Perl

Basic Syntax of Perl

Comments

begin with #dont try to use // or /* ... */

Print

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

Page 28: 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

Page 29: 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

Page 30: 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

Page 31: 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

Page 32: 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

Page 33: 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

Page 34: 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

Page 35: 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

Page 36: 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

Page 37: 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

Page 38: 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

Page 39: 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

Page 40: 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

Page 41: 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

Page 42: 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

Page 43: 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

Page 44: 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

Page 45: 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

Page 46: 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

Page 47: 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

Page 48: 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

Page 49: 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

Page 50: 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

Page 51: 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

Page 52: 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

Page 53: 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

Page 54: 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

Page 55: 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

Page 56: 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

Page 57: 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

Page 58: 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

Page 59: 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

Page 60: 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

Page 61: 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

Page 62: 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

Page 63: 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

Page 64: 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

Page 65: 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

Page 66: 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

Page 67: 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

Page 68: 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

Page 69: 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

Page 70: 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

Page 71: 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

Page 72: 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

Page 73: 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

Page 74: 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

Page 75: 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

Page 76: 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

Page 77: 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

Page 78: 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

Page 79: 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

Page 80: 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

Page 81: 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

Page 82: 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

Page 83: 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

Page 84: 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

Page 85: 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

Page 86: 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

Page 87: 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

Page 88: 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

Page 89: 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

Page 90: 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

Page 91: 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

Page 92: 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

Page 93: 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

Page 94: 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

Page 95: 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

Page 96: 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

Page 97: 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

Page 98: 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

Page 99: 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

Page 100: 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

Page 101: 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

Page 102: 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

Page 103: 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

Page 104: 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

Page 105: 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

Page 106: 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

Page 107: 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

Page 108: 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

Page 109: 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

Page 110: 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

Page 111: 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

Page 112: 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

Page 113: 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

Page 114: 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

Page 115: 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

Page 116: 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

Page 117: 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

Page 118: 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

Page 119: 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

Page 120: 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

Page 121: 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

Page 122: 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

Page 123: 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

Page 124: 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

Page 125: 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

Page 126: 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

Page 127: 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

Page 128: 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

Page 129: 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

Page 130: 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

Page 131: Perl

Control Structures - ForEach Loop

Syntax 3:

foreach $i (a,b,c)

{

print uc $i;

}

Sreejith S Let’s Learn Perl

Page 132: Perl

Control Structures - ForEach Loop

Syntax 3:

foreach $i (a,b,c)

{

print uc $i;

}

Sreejith S Let’s Learn Perl

Page 133: 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

Page 134: 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

Page 135: 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

Page 136: 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

Page 137: 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

Page 138: 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

Page 139: 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

Page 140: 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

Page 141: 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

Page 142: 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

Page 143: 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

Page 144: 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

Page 145: 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

Page 146: 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

Page 147: 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

Page 148: 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

Page 149: 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

Page 150: 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

Page 151: 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

Page 152: 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

Page 153: 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

Page 154: 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

Page 155: 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

Page 156: 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

Page 157: 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

Page 158: 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

Page 159: 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

Page 160: 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

Page 161: 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

Page 162: 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

Page 163: 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

Page 164: 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

Page 165: 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

Page 166: 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

Page 167: Perl

Web Resource for Perl

Web resources for Perl

www.perl.comwww.perl.orgwww.perlmonks.org

Sreejith S Let’s Learn Perl

Page 168: Perl

Web Resource for Perl

Web resources for Perl

www.perl.comwww.perl.orgwww.perlmonks.org

Sreejith S Let’s Learn Perl

Page 169: Perl

Recommended Reading

Sreejith S Let’s Learn Perl

Page 170: Perl

Questions

Sreejith S Let’s Learn Perl

Page 171: Perl

Thank You

Sreejith S Let’s Learn Perl


Recommended