Intro To Perl
Intro To Perl
Introduction to Perl
Deepak
Chopade
Day 1
Introduction to Perl
Scalar Variables
I/O Functions
Operators
String and formatting strings
Selectional, iterational and miscellaneous control statements
Arrays and various operations on arrays
Day 2
Hashes and various operations on hashes
Regular Expressions
Meta characters
Character classes
Pattern matching
Subroutines
Command line arguments
Day 3
File Input/Output
Various file opening modes
Database Programming using DBI & DBD
Introduction to CGI Programming using Perl
Introduction to Perl
Scalar Variables
I/O Functions
Operators
statements
Arrays and various operations on arrays
Perl Interpreter
Converts the scripts in to a parse tree and executes immediately
Internally Perl maintains a byte code for execution
Known as interpreter/ compiler
Scalar Variables
Simple variables containing an element, either a number or a string
Defined by $ symbol
Array
List of scalar variables
Defined by @ symbol
Hashes
Similar to arrays
Each item is identified by a key value
Defined by % symbol
E.g. 34000.50
warn
Outputs a message on the STDERR stream (unbuffered)
Used to trace error when STDOUT is redirected
Program is neither terminated nor an exception is thrown
warn “Message”
Assignment Operator
Auto increment and decrement operators
Additive operators
Multiplicative operators
Exponentiation Operator
Relational Operators
Logical Operators
Range Operator
Binding Operator
Arrow operator
File I/O Operator
$Number1 = 5;
Compound assignment
$Number2=$Number1; 6
Auto increment and decrement operator (++, --)
$Number3=$Number2=$Number1;
Shivkumar
$Number=5;
Additive Operator (+, -, .)
print ++$Number;
Addition & Subtraction operator
print $Number++;
Concatenation Operator (.)
$x=4;
$y=3;
print $x ** $y;
- Logical Operator
AND - && or ‘and’
OR - || or ‘or’
NOT - not
Intro to Perl 1-25
PERL – Operators contd
Range Operator (..)
• Returns a list of sequence of values
print (1..50);
print (a..z);
Bind Operator (=~, !~)
• Used for binding scalar expressions to a pattern match
Arrow Operator (->)
• Used to retrieve elements from a reference
File Operator (<File_Handle>)
• Used for File I/O Operations
• <> represents <STDIN>
Input operator
<STDIN> or <>
Read input from the user including ‘\n’ from the user
chomp operator can be used for removing the ‘\n’ character
By default input is stored in a default variable $_
$No=<STDIN>;
$Name=<STDIN>;
20Ajeesh
print $No;
print $Name;
chomp $No;
chomp $Name;
print $No;
print $Name;
$Dir="/etc/bin";
$FileName=$Dir."/".$Name.$Extention;
print $FileName;
Repetition Operator
$RandFancyNum="5" x 3;
‘x’ makes as many number of concatenated copies of string
print $RandFancyNum;
NotePad.EXE
$FileName= "NotePad.EXE";
Converting initial case to lower
$FileName=lcfirst using
$FileName; – lcfirst STRING
print $FileName;
$FileName= "NotePad.EXE";
$FileName=ucfirst $FileName;
Converting initial case to upper using – ucfirst STRING
print $FileName;
Intro to Perl 1-31
PERL – String Functions contd
9
Getting string length using – length (STRING)
Returns the length of the string
If STRING is omitted, it returns the length of the string stored in $_
$FileNameDOS="MyApp.Exe";
$Length=length ($FileNameDOS);
print $Length;
index (STRING,
Searching string using –
SUBSTRING, [POSITION])
returns the index of the first occurrence of the SUBSTRING in STRING on or after
POSITION
If POSITION is ignored, it starts searching from the first location
returns -1 if not found
rindex can be used to find the index of the last occurrence of the SUBSTRING in
STRING on or before POSITION
Using$Domain="www.ad.infosys.ad.com";
index 15
$Result=index($Domain,"ad");
print $Result;
print "\n";
$Result=index($Domain,"ad",13); 15
print $Result;
4
$Domain="www.ad.infosys.ad.com";
Using$Result=rindex($Domain,"ad");
rindex
print $Result;
print "\n";
$Result=rindex($Domain,"ad",13);
print $Result;
Extracting substring:
$Source="http://www.infosys.com:8080/index.html";
$Protocol=substr($Source,0,4);
print $Protocol;
$Source="www.infosys.com";
Reversing the String:
$Image= reverse($Source); moc.sysofni.www
print $Image;
Branching Statements
if
if – else
if – elsif
Looping Statements
while / until
do – while/ until
for
foreach
$FileSize=<STDIN>;
if($FileSize==-1) {
print "File Not Found!\n";
}elsif($FileSize==0) {
print "File Empty!\n";
}else {
print "Size of the file is
$FileSize";
}
while loop
while (condition) {
#while block statements
}
Control comes out of the loop when the condition is FALSE
until loop
until (condition) {
#until block statements
}
Control comes out of the loop when the condition is TRUE
Solution?
Expression Modifiers
‘if’ modifier can be tagged to an expression. Expression is evaluated only if the
condition is true
expression if (condition);
exit
used to terminate the execution of a script
exit 0 denotes success and exit 1 denotes failure
cannot display error messages while exiting
$Choice=<STDIN>
if ($Choice==4) { exit 0};
die
displays the error message to STDERR stream
and terminates the execution of a script
Creation
@EmpName = (“John”,”Dave”,”Mary”,”Jim”);
@EmpNumber = (1001,1005,1008,1009);
$EmpName[0] = “John”;
Or
$EmpName[1] = “Dave”;
$EmpName[2] = “Mary”;
$EmpName[3] = “Jim”;
for($Loop=0;$Loop<5;$Loop++) {
$Number[$Loop] = <STDIN>;
Or
}
Intro to Perl 1-49
PERL – Arrays contd
print @Number;
Toprint
print thejoin(‘,’,@Number);
individual elements
101102103104
print $Number[0];
print $Number[1];
- To print
print the $Number[2];
elements using a loop 12345
print $Number[3];
@Emp=(1..5);
foreach $i (0..4) {
print $Emp[$i];
print " ";
}
Emptying an array
$#Emp = -1;
OR
@Emp = ( );
OR
@Emp = undef;
101,102,103,104,
Merging of arrays using push
@EmpOld=(10,11,12); 1150,1151,1152
@EmpNew=(105,106,107);
push (@Emp,@EmpOld); 101,102,103,104
push (@Emp,@EmpNew);
print join(',',@Emp);
1150,1151,1152
@Emp=(101,102,103,104,1150,1151,1152);
Array Slices (Section of an array)
print join(',',@Emp);
@Senior=@Emp[0..3];
print join(',',@Senior);
@Junior=@Emp[4..6];
print join(',',@Junior);
Scalar Variables
Perl List
Display Functions
Operators
Standard input using <STDIN>/<>
Strings and various string functions
Conditional constructs
Looping constructs
Miscellaneous control statements
Arrays
Any questions?
Thank you!