Upload
rakesh-mukundan
View
686
Download
0
Embed Size (px)
DESCRIPTION
Learn perl with exampleswww.rakeshmukundan.in
Citation preview
Practical Approach to PERL
Rakesh Mukundan
Scripting Language Uses an interpreter to run the code No compilation needed just run it Interpreted line by line Fast to learn and program Easy debugging Every user is a developer )
PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable
bull Began as the result of one mans frustration and by his own account inordinate laziness
bull Perl is free The full source code and documentation are free to copy compile print and give away
Why Learn Perl
bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive
work
Do You Have Perl Installed
bull Execute the following command in a shellndash perl -v
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Scripting Language Uses an interpreter to run the code No compilation needed just run it Interpreted line by line Fast to learn and program Easy debugging Every user is a developer )
PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable
bull Began as the result of one mans frustration and by his own account inordinate laziness
bull Perl is free The full source code and documentation are free to copy compile print and give away
Why Learn Perl
bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive
work
Do You Have Perl Installed
bull Execute the following command in a shellndash perl -v
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
PERLbull Practical Extraction and Report Languagebull Also known as Practically Everything Really Likeable
bull Began as the result of one mans frustration and by his own account inordinate laziness
bull Perl is free The full source code and documentation are free to copy compile print and give away
Why Learn Perl
bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive
work
Do You Have Perl Installed
bull Execute the following command in a shellndash perl -v
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Why Learn Perl
bull Perl is easy and it makes life easybull Its open sourcebull Lots of tested modules available for re-usebull You are too lazy to do mechanical repetitive
work
Do You Have Perl Installed
bull Execute the following command in a shellndash perl -v
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Do You Have Perl Installed
bull Execute the following command in a shellndash perl -v
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Installationbull UNIX and Linux Available with installation CD or
standard repositories if not installed by default
bull Windows Many flavors are available with and without IDEs free and proprietary etcndash Active State Perlndash Strawberry Perl
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Your First Perl Programbull Open MyFirstProgrampl from the examples directory
with your favorite text editor ( geditvimnotepad++ etc )
bull To run the program open up a shell and navigate to the examples directory bull perl MyFirstProgrampl
bull In Linux systems perl file can be directly executed provided path to interpreter has been specified correctlybull chmod a+x MyFirstProgramplbull MyFirstProgrampl
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Under the hood
A perl statement terminated with a semi-colon
Comment
Location of interpreter
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Variables
bull Place to store databull A scalar variable stores a single valuebull Perl scalar names are prefixed with a dollar
sign ($)ndash Ex $name$password$ip_address
bull No need to define a variable explicitly use directly
bull A scalar can hold data of any type be it a string a number or whatnot
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Examples of Variablesbull $floating_val = 314bull $integer_val = 1008bull $string_val = ldquoMy Stringrdquobull $hashReff = HashToPointbull $SubReff = MyRoutine()bull $ScalarReff = $floating_val
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Numerical Operations
Addition $var1 + $var2Subtraction $var1 - $var2Multiplication $var1 $var2Division $var1 $var2Increment $var++Power $var1 $var2Modulus $var1 $var2
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Contd Greater than $var1 gt $var2 Greater than or equal $var1 gt= $var2 Less than $var1 lt $var2 Less than or equal $var1lt= $var2 Equality $var1== $var2
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Strings
An string of characters no size limit ldquoHello Worldrdquo
Can be specified using single quotes() or double quotes(ldquo)
Strings can be concatenated using dot () operator
No operations are possible inside a single quoted string
ldquo1 plus 2 is $valuerdquo 1 plus 2 is $value
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Special Characters L Transform all letters to lowercasel Transform the next letter to lowercaseU Transform all letters to uppercaseuTransform the next letter to uppercasenBegin on a new liner Apply a carriage returnt Apply a tab to the stringEEnds U L functions
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Print Function
Most commonly used perl functionCan print a variable or string to
consolefileany file handleUsage print ltfile handlegt expressionBy default prints to STDOUT
Print ldquoHello World nrdquo Print $MyVariable Print ldquoMy name is $MyVariable nrdquo Print ldquoOne plus one is always 1+1 nrdquo
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
User Input ltSTDINgt stands for standard input Program waits for user to enter an input It will contain newline character also $MyAge= ltSTDINgt chomp($MyAge)
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Calculator Program
Ask user to enter two numbers Do all the numerical operations mentioned in
previous slide and print the output
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
String Operations
index(STRSUBSTR) Returns the position of the first occurrence of SUBSTR in STR
length(EXPR) Returns the length in characters of the value of EXPR
rindex(STRSUBSTR) Works just like index except that it returns the position of the LAST occurrence of SUBSTR in STR
substr(EXPROFFSETLEN)Extracts a sub string out of EXPR and returns it
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Arraybull List of scalarsbull Similar to arrays in CC++ etcbull Array is identified by symbolbull FirstArray = (ldquoonerdquordquotwordquordquothreerdquo)
bull Each element can be accessed by its corresponding indexbull print $FirstArray[2]
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Arrays
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Visualizing Data In Perl Use the Dumper module use DataDumper print Dumper $ref $ref is the refference to the variable print Dumper Array print Dumper Hash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Working With Arrays
bull An array element can be indexed as $MyFirstArray[1]
bull push() - adds an element to the end of an array
bull unshift() - adds an element to the beginning of an array
bull pop() - removes the last element of an arraybull shift() - removes the first element of an
array
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
A phonebook
bull Pallava =gt 3001bull Krishna =gt 3002bull Godhavari =gt 3003bull Kaveri =gt 3004
bull How do you represent such a listndash Lookup by names
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Hashesbull Hash is a like a phone book which has names
and corresponding phone numbersbull Each element in a hash will have a key and
valuebull For examplendashPhoneBook = (bull ldquopallavardquo =gt 3001bull ldquokrishnardquo =gt 3002bull ldquogodhavarirdquo=gt 3003bull ldquokaverirdquo =gt 3004)
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Hashes
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Working With Hash Valuesbull Each value can be accessed by its keyndash $Phonebookldquopallavardquo
bull To add a new value to hash tablendash $Phonebookldquonilgirisrdquo =rdquo3005rdquo
bull To delete a value from hash tablendash delete($Phonebookldquonilgirisrdquo)
bull Looping through a hashtablendash while (($key $value) = each(Phonebook))ndash Print $key$valuendash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Contd
bull Checking if a particular key has already added in the hash tablendash if (exists($Phonebookpallava))ndash ndash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Program Control
bull Ifbull Whilebull Forbull Foreach
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
ifbull Conditional statement to check if a criteria is
met or notbull The syntax is
bull if(condtion) code to executebull if($var1 ==5)
bull print ldquovariable is 5nrdquobull
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
ifelse Else is the compliment of if Execute code if the condition is not met Syntax
if(condition) code for condition met else code for condition false
if($var1==5) print ldquovariable value is 5nrdquo
else
Print ldquovariable value is not 5nrdquo
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
elsif
if(condition1) Code to execute
elsif(condition2) Code to executeElse
Code to execute
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
whilebull Loop while the condition is truebull while($countlt5)
bull Print ldquoCont$countnrdquobull $count++
bull bull While(1) makes an infinite loopbull Flow Control
bull next go to the next iterationbull lastend while loop
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
forbull A for loop counts through a range of
numbers running a block of code each time it iterates through the loop
bull for(initial condition $increment) code to execute
bull for($count=0$countlt11$count++)
bull Print ldquoCount $count nrdquobull
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
foreachbull Used for iterating over an array or hashbull foreach(MyArray)
bull print ldquoElement $_nrdquobull
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Phonebook Program Print existing numbers Option to add a new entry Option to delete and entry Option to exit the program Option to search by name
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash
Strict UsageBy default perl doesnt need any variable to be
declared before useSimple spelling mistakes in variable names can
lead to hours of code debuggingBy using the strict methodperl will strictly ask
you declare variable my $MyFirstVar my MyFirstArray my MyFirstHash