Perl

it is a programming language designed for text processing by Larry wall. perl stands for "Practical Extraction and Report Language".it works on WINDOWS, MAC OS, and different versions of UNIX.

Installation of perl:
perl comes inbuilt in LINUX, however for WINDOWS we have to install either one of the following perl flavours.
  1. Activestate perl
  2. Strawberry perl
  3. Dwim perl
Dwim perl also provides IDE (integrated development environment).
to check whether perl is installed or or not into system open the command prompt(in WINDOWS) or terminal(in LINUX) and type 'perl  -v'.
it will display the brief information about the version and providers of perl.
by typing 'perl  -V ' you will get elaborative information about installed perl.
Fig 1: version information of perl





 the above description in terminal(i.e., in LINUX) will look like this.,
FIg 2: Desrption about perl in terminal

to  get the basic help regarding commands of perl type 'perl -h' or 'perl -help' commands.
i.e.,
Fig 3: help information on perl

Note:
  • dear friends, don't get confused with the above screen it is not command prompt but it is 'Powershell'( introduced from WINDOWS 7 onwards) which gives much advanced features over command prompt.
  • "Command prompt is a legacy while powershell is a future"
  • so dear friends, why you are waiting start exploring Powershell.
  • you can also check whether perl s installed or not in C drive. if  it is installed you will see a folder with the name perl.
  • next and last keywords works like continue and break in C language
now let us learn some basic concepts about perl before stepping into actual programming experience.

COMMENTS :
  • #   -> to create comment. (it is same in mysql and R)
  •  $[var_name] ->to create varibles.
  • to interpret variables,arrays,hashes or escape sequences you have to enclose between double inverted commas.
e.g
$scalar1='a';
$scalar2='123';
$scalar3='atgctgcaatgccgta';
@array=('seeta-rama','nala-damayanti','romio-juliot','radha-krishna');
%hashe=(A=>"apple",B=>"ball");

print "$scalar1,\t $scalar2\n";
print "first element in array: @array[0]\n";
print "second value of hashe is : %hashe{1}"; 

ESCAPE SEQUENCES:
  • it is a combination of characters, with alphabets and having special meaning.
  • if we assign varibles inside " " we can render escape sequences otherwise they won't work.
  • \"    shows this is not end of the command line.
  • \a     create sound.
  • \t     horizontal tab eqvivalent to 5 space characters.
  • \u    changes the case of very next character to upper case but converse is not true.
  • \U    changes all following characters to upper case.
  • \l    opposite to above escape sequence.
  • \L    changes all following characters to lower case.
  • \Q    when special characters are followed.
e.g
#!/usr/bin/perl
$my_var= "5 is different from \"five\"\n";
print $my_var;
print "Do you hear the bells?\a\n";
print "Do yoy see the ga\t\tp?\n";
print "Te odd \uone ouy\n";
print "MY \lNEW GUITAR!\n";
print "the next \Ucase\n";
print "THE LAST \LCASE\n";
print "\Q#$%^^&*@!";


SCALAR VARIABLES:
  • single unit of data.
  • in perl it is just scalar.
  • scalar variables are preceded by '$' symbol.
  • can hold numeric,string,float,hexadecimal values or characters.
  • string varibles can be created either using single or double quotes. (but, to include escape sequences we have to use later ones.)
  • octal numbers will precede with '0'.
  • hexadecimal numbers with '0x'.
  • decimal as it is.
  • octal with 'o'.
  • binary with '0b'
e.g.,
#!/usr/bin/perl -w
$i = 1;
$j = "2";
print "$i and $j \n";
$k = $i + $j;
print "$k\n";
print $i . $j . 3 ."\n";
print'kannada' . "\n";

STANDARD INPUT:
to get the information from user we have to use he following code.,
$var= <STDIN>;
* DIE kills your script safely and prints the message.

e.g.,
# RECIEVING INPUT FROM USERS:
#!/usr/bin/perl
print "Enter a number:/n";
$num=<STDIN>; # it is a stream
$square=$num*$num;
print "The square of $num is: $square/n";



ARRAYS:

  • stores ordered list of scalar values.
  • we can put numbers and also strings.
  • '@'is preceded to create array variables.
  • values have to be placed within paranthasis and to be seperated by ','.
  • string varibles must be enclosed between ' ' or " " .
  • index of first element is 0 and it follows...
  • to print particular variable from array array variable is followed by index i.e., array[index i.e., starts from 0] 
e.g., 
#!/usr/bin/perl
@ranks=(1,2,3,4,5);
@names=('mohan','nandita','subin','dipro');
print "All ranks : @ranks\n";
print "All names: @names\n";
print "Third name: @names[2]\n";
print "last 3 names: @names[1,2,3]\n";
print "last name: @names[3]\n";
print "first name: @names[0]\n";


ARRAY operations:
  • we can use @array(1..10);    for sequential filling of array variables from 1 to 10.
  • but remember we have to use dots not comma. (size is 10)
  • similarly to fill alphabets we use (a..z; to fill a to z. size is 26)
  • we can assign array variable to a scalar variable.
#!/usr/bin/perl
@ranks=(1..10);
@alphabets=(a..z);
print "All ranks: @ranks\n";
print "All alphabets: @alphabets\n";
print "9th alphabet: @alphabets[8]\n";
$size_alphabets=@alphabets;
$size_ranks=@ranks;
print "Size of alphabets array :$size_alphabets\n";
print "Size of ranks: $size_ranks\n";


ADDING and REMOVING Array elments:

  • 'push'        command to insert new element into array; but adds at the end.
  • 'unshift'    similar to above but adds at the first.
  • 'pop'        remove last array element.
  • 'shift'        shifts out first element from array elements.
e.g.,
#!/usr/bin/perl
@players=("Mohan","Nandita");
print "players set one: @players\n";
push (@players, "subin"); # Add element at end
print "players set two: @players\n";
unshift (@players, "Dipro"); # Add element at the begining
print "players se3: @players\n";
pop(@players); #Remove element from the end.
print "players set4: @players\n";
shift(@players); #Remove element from the begining.
print "players set 5: @players\n";


'join' combines the elements of an array into a single common variable (i.e., a string).
'Loops' are useful with arrays.
e.g.,
#!/usr/bin/perl
@instruments=('guitar','piano','tabala','mrudanga','flute','violin');
print "Instruments array: @instruments\n";
$my_string=join('&&', @instruments);
print "Instruments string: $my_string\n";


SLICING Arrays:
that is printing specific parts of an array.
Note: we can use escape sequences within the array variables also.

e.g.,
!/usr/bin/perl
@string_arr=('1','8','3','4','5','6','7');
print "Original string list: @string_arr\n";
@string_newarr=sort(@string_arr);
print "sorted string list: @string_newarr\n";
@arr1=(1..5);
@arr2=(6..100);
@final_arr=(@arr1, @arr2);
print "Merged  array: @final_arr\n";


REPLACING Array Elements:

  • here we will replace array elements by other elements.
  • perl functions are also called as sub-routines.
  • we can use any number of ',' s. it won't distructs output.
e.g.,
#!/usr/bin/perl
@actors=('darshan','sharukh','amithab','amir khan','salman khan','rajkumar','rajani kanth');
@new=('vishnu','Ravi');
print "actors: @actors\n";
splice (@actors, 1,2, @new);
print "new list: @actors\n";
 



STRING TO ARRAY COVERSION:

  • we use 'split' rotine to do exact opposite thing of last script.
  • if we use ' ' i.e., single inverted commas with no space, it will display seperated characters. here it will take space also as a character.
  • if we use ' ' i.e., single space between inverted commas it will display separate words as strings.
  • if space is more than single space it will display all the strings.
e.g.,
#!/usr/bin/perl
#here we use split method.
$my_string="God is present in every Jeeva so if  you serve jeeve you intern serve God";
@my_array=split('', $my_string);
print "text array: @my_array\n";
print "@my_array[0..9]\n";
 

SORT and MERGE ARRAYS:

  • 'sort'    subroutine accept array as an argument and sorts it and stores result into other or same array.
  • it will sort string variables in alphabetical order.
  • but there should be no space after ',' inside array.
  • if we use numeric values it will sort in increasing order.
e.g.,
#!/usr/bin/perl
@string_arr=('one','two','three','four','five','six','seven');
print "Original string list: @string_arr\n";
@string_newarr=sort(@string_arr);
print "sorted string list: @string_newarr\n";
@arr1=(1..5);
@arr2=(8..100);
@final_arr=(@arr1, @arr2);
print "Merged  array: @final_arr\n";
 

HASHES:

  • HASH is a set of key value pairs.
  • e.g., diffrent components in phone book.
  • we use '%' symbol to create HASHES.
  • key value ralation can be represented inside HASH as (1=>"novak", 2=>"roger");
  • here 1, 2 are keys for novak and roger scalar values respectively.
  • similarly we can give countries as keys and capitals as values.
  • there is no need to put numerals inside quotes but for strings it is compulsory.
  • $hashname{key for which value is needed}    to get the value corresponding to the key from hash.
e.g.,
#!/usr/bin/perl
%students=(1=>"Nandita",2=>"dipro",3=>"subin",4=>"mohan");
%places=('Nandita'=>'delhi','mohan'=>'karnataka','subin'=>'kerala','dipro'=>'kolkata');
print "first enrolled student: $students{1} \n";
print "place of Subin: $places{'subin'}\n"; 



SLICING HASHES:

  • we have to enter our index values within braces but not paranthesis.
  • to get particular elements separate them by','.
  • to get range separate them by '..'
  • if we give the range more than we are having in array it will consider only array elements. but it won't show any error.
e.g.,
#!/usr/bin/perl
%friends=(1=>'lokesh',2=>'Nandita',3=>'tharun',4=>'vinay');
@my_friends=@friends{2,4};
print "my friends: @my_friends\n"; 


OBTAINING KEYS AND VALUES FROM HASHES:

  • we obtain keys from hash inputs.
  • we use 'keys' subroutine.
  • for values we use 'values' keyword.
e.g.,
#!/usr/bin/perl
%life_battle=(1=>'set the goal',2=>'acquire knowledge continuously',3=>'hard work with dedication',4=>'perseverance');
@priority=keys %life_battle;
@things_needed=values %life_battle;
print  "keys: @priority\n";
print "values: @things_needed\n";


CHECK ELEMENT REXISTENCE:

  • we use 'exists' subroutine here.
  • we use 'if else' construct here.
Note:we can use any number of print statements under 'if' block.

OBTAINING SIZE OF HASHES:

  • it is number of key value pair in the hashe.
  • here we use 'hashe', 'array', and also 'scalar'. to obtain the size of hashe. 
e.g.,
#!/usr/bin/perl
%fear=(1=>'face',2=>'everything',3=>'and',4=>'recover');
@list=keys %fear;
$size=@list;
print "Number of words in the expansion of FEAR: $size\n"; 


ADDING AND REMOVING HASH ELEMENTS:

  • here we learn how to add or remove key value pairs.
  • here also we use 'hash';'array',and 'scalar'.
  • if we use 2 or more similar keys than it will be considered only one time.
  • preferably the names for hashes and arrays must be different.
  • otherwise entire thing in hash is ignored.
Note: perl is case sensitive.
e.g.,
#/usr/bin/perl
# our sentences must be KISS
# i.e., Keep It Short and Simple.
#let us work on KISS.
%KISS=(K=>'keep', I=>'it', S=>'short', S2=>'simple');
@words=keys %KISS;
$size=@words;
print "total number of words in KISS is: $size\n";
$KISS{5}='that is our sentences';
@words=keys %KISS;
$size=@words;
print "words after inclusion of 5th word is: $size\n";
delete $KISS{5};
@words=keys %KISS;
$size=@words;
print "words after exclusion of 5th word is: $size\n";


IF STATEMENT:

  • it is  important in general, in any programming language.
  • it is a decision construct.
  • first we have to write condition.
  • e.g., the given value is even or not.
  • ==    it is not assignment but it is 'equal to'.
  • we could have bunch of print statements in if block base on the condition.
  • similarly we can include many number of conditions.
e.g.,
#!/usr/bin/perl
$x=42;
$y=10**2;
if ($x%2==0)
{
    print " x is even\n";
    print"the square of 10 is: $y";
   
}
else
{
print "X is odd\n";
}


IF ELSE Statement:
 in addition to above we just add 'else' block here.

e.g.,
#!usr/bin/perl
$y=47;
if($y%2==0)
{
    print "y is even\n";
}
    else
    {
        print "y is odd\n";
    }
    


ELSE IF Statement:

If  and IF ELSE Statements allows to make decision which involves one or 2 choices.
in the case of more decisions we use this statement.
we use 'eq' operator to compare string variables stored in variable.
e.g.,
#/usr/bin/perl
$op="add";
$x=20;
$y=10;
if($op eq "add")
{
    $res=$x+$y;
    print "Result: $res\n";
}
elsif($op eq "sub")
{
$res=$x-$y;
print "result:$res\n";
}
elsif ($op eq "mult")
{
    $res=$x*$y;
    print"result: $res\n";
}
elsif($op eq "div")
{
$res=$x/$y;
print "Result: $res\n";
}
elsif ($op eq "rem")
{
$res=$x%$y;
print "Result $res\n";
}
# dear friends the above  script is a 5 function calculator.
# to work with particular function we have to select the variable 'op' first.
#i.e., either 'add','sub','mult','div',and 'rem'.


UNLESS Statement in Perl.

syntaxis very similar to IF statement.
but alternative to If statement.
if test condition evaluates to be false then condition in unless block will work.

e.g.,
#!/usr/bin/perl
$x=10;
unless($x%2==0)
{
    print "x is odd\n";
}
else{
print "X is even\n";
}


UNLESS ELSE Statement:

it is extension of unless statement.
similar to if else over if.

e.g.,
#!/usr/bin/perl
$x=10;
unless($x%2==0)
{
    print "x is odd\n";
}
else
{
    print "x is even";
}


CONDITIONAL OPERATOR:

substitute for IF ELSE statement.
it is slight confusing.
similar to conditional operator in any other programming language. follow the below mentioned steps.
  • create scalar varible.
  • type test condition.
  • if condition is true we do onething otherwise the thing contrary to it.
e.g.,
# COMPARISON OPERATORS
# upcourse we used with IF and while statements.
# these are also called as relational operators.
#!/usr/bin/perl
$x=20;
$y=15;
if($x==$y)
{
print "$x and $y are equal\n";
}

if($x!=$Y)
{
print "$x is not equal to $y\n";
}

if($x<$y)
{
print "$x is less than $y\n";
}

if($x>$y)
{
print "$x is greater than $y\n";
}

if($x<=$y)
{
print "$x is less than or equal to $y\n";
}

if($x>=$y)
{
print "$x is greater than or equal to $y\n";
}


WHILE LOOP:

it allows to execute bunch of statements multiple number of times.
types: While    Do While    For
1.While loop:
it involves the following steps.
  • Create scalar variable.
  • display value of variable.
  •  update value of varible.
  • give condition within the loop.
  • body of loop within braces.
e.g.,
#/usr/bin/perl
$x=10;
while($x<20)
{
    print "$x\n";
    $x=$x+1;
}


UNTILL LOOP:

it is exact opposite of while loop.
here we keep on doing as long as test conditon is false.
we use 'until' keyword instead of 'while'.

e.g.,
#!/usr/bin/perl
$x=10;
until($x>20)
{
print "$x\n";
$x=$x+1;
}



UNLESS ELSE:
this is opposite to if else statement.
e.g.,
$weather= 'cool';
$Rain ='moderate';
unless ($weather ne $Rain) {


print "Dress as you wish\n";


}


else {


print " use Umbrella\n";




 
How to run the scripts...?

in case of  IDE you just type script with the extension '.pl' and click on 'Run' option to get the output.
but running scripts using Command prompt/Powershell (in WINDOWS), or terminal(in LINUX) involves following steps.

  1. create perl script using text editors like Vi, Vim, gedit, emacs in LINUX and Notepad and Notepad++ in Windows with the extension .pl.
  2. open Powershell or terminal and type 'perl  script.pl'
  3. press enter.
  4. if the script is correct it will display the output. otherwise,through the error message.
i.e.,
Fig 4: script and  output for if.pl script
 script for string manipulation in perl:
string is a combination of characters, numbers or symbols. the string manipulation operations in perl are.,
  1. Concatenation
  2. Interpolation
  3. String length
  4. converting case
  5. Indexing of a string
  6. Extracting substring and replacing with other.
  7. Split and Join
  8. Repetation 
e.g., for all the above operations in one script is shown below.,
#!usr/bin/perl -w#STRING MANIPULATION:

print"\n\n*****1. concatenation *****\n";
$str1="Mohanbabu";
$str2="H S";

$concatenated_string=$str1.$str2;
print $concatenated_string;
print "\n$str1"."$str2\n\n";

print"***** 2.Interpolation *****";
# i.e., we can use the string variable inside our print statement.
print"\n20800 is the roll_no of $str1 $str2\n\n";

print "***** 3.String_length *****\n";
$name=Nandita;
$length_of_string=length($name);
print "length of a string is :$length_of_string\n";

$seq1="atgcatgc";
$seq2="ATGCATGC";
$length_of_sequence=length($seq13);
print "length of a string is :$length_of_sequence\n\n";

print "***** 4.Converting the case *****\n";
$upper=uc($seq1);
print "$upper\n";
$upper=ucfirst($seq1);
print "$upper\n";
$lower=lc($seq2);
print "$lower\n";
$lower=lcfirst($seq2); # concept of overwriting variable
print "$lower\n\n";

print "***** 5.indexing of a string *****\n";
$index=index($seq1,"atg");
print " the first position of the string is:$index\n";
$index=rindex($seq1,"atg");
print " the last position of the string is:$index\n\n";


print "***** 6.Extracting substring and replacing with other *****\n";
$seq_mod=$seq1;
$sub_seq=substr($seq_mod,0,5,'NNNNN');
print "$seq1\n";
print "$seq_mod\n";
print "$sub_seq\n\n";



print "***** 7.Split and Join operations *****\n";
$TEAM="Together Everyone Achieve More";
($w1,$w2,$w3,$w4)=split(/ /, $TEAM );
@TEAM=split(/ /, $TEAM );
print "$w1\n";
print "$w2\n";
print "$w3\n";
print "$w4\n\n";
print "@TEAM\n";
print "so dear friends, give value for TEAM work. \n\n";

$joined=join(" ",("Eliminating", "Growth", "Oppurtunity"));
print "$joined\n";
print "so dear friends, leave your EGO...\n\n";
$words="a title of a web page";
$words=join ' ',
map {ucfirst lc}split / /, $words;
print "$words\n\n";

print"***** 8.Repetation ******\n";
$dash="RAMA"x20;
print"$dash\n\n\n\n\n";

print "***************************************THANK YOU***********************************************";


Note:
Vi is  a text editor which opens in terminal of LINUX. some useful commands and instructions for effectively operate in Vi editor are as follows.,


vi newfile -opens new file
:q -to quit
:q! (forced quit)

modes: 
 
i -input mode
Esc -to go back to command mode

navigation in command mode: 
 
h -move left one character
j  -move down one character
k -move up one character
l -move right one character

useful commands in command mode: 
 
dd -for deleting
yy  -for copying
p  -for pasting
:r filename  -for reading text from another file.
/search-text  -for searching top to bottom.
?search-text  -for searching bottom to top.
n -follow search from top to bottom.
N - reverse of above.
r -for replacing the test under cursor.
u - undo
O and o -add new line above or below cursor respectively.

*we need to type colon for executing any command.
:w -for saving the current line.
:w! -for saving the linr forcefully.
:wq -for saving and exiting the file.
:wq! -for saving and exiting the file forcefully.
:history -for history
$ -for end of the line
^ -for begining of the line
:1 -for the first line
G -for the last line of the file.
:n -for nth line number.
. -for repeating the command
:!ls -ltrh -for executing the external command from vi
:r filename  :reads data from given file name.
:%s/abc/xyz/g -for relacing xyz in place of abc from current line.
:r ! ls -ltrh  -for reading output of ls -ltrh into the current line.
:sp -for horizontally splitting the current file.
:vsp  -for vertically splitting the current file.































Comments