DOCUMENTTYPE 1 (22) TypeUnitOrDepartmentHere TypeYourNameHere TypeDateHere PERL Training ± 5 th to 7 th OCT 2010 Day 1 : Scripting is mostly use din automation, Coding ( programming ) lang needs to be complied and intermediate code is generated NO DATA TYPES in scripting , hence kind of data can b e used . Text processing is easy with scripting ( regular expression in automation) PERL : Pratical Extraction & Report Lauguage For details : ³Cpan.org´ ± Inbuilt modules are present , you can use from here. Scripting : Types of data : $ scalar@ list % associate array PRINT & STDIN : #!C:/PERL/bin/PERL51~1.EXE ±w Print ³enter your name´; $name=<STDIN> Print ³Hello, @name´; #interpolation CHOP: chop($name);# chop will chop ANY last character at the end When user clicks on ENTER , it takes as ³CARRIAGE RETURN´ Hence after you input any data in <STDIN>, It will also take in LAST character as ENTER ( one lone of blank data ) SO use CHOP to take only the exact data required Arithmetic operation : All input by default is a STRING µ+¶ always converts any input to NUMERICAL values ALWAYS. So when you give a string say ³abc´ , its value converts to ZERO , As µ+¶ operator converts to numerical.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Scripting is mostly use din automation,Coding ( programming ) lang needs to be complied and intermediate code is generated
NO DATA TYPES in scripting , hence kind of data can be used .Text processing is easy with scripting ( regular expression in automation)
PERL : Pratical Extraction & Report Lauguage
For details :
³Cpan.org´ ± Inbuilt modules are present , you can use from here.
Scripting :
Types of data :
$ scalar @ list% associate array
PRINT & STDIN :
#!C:/PERL/bin/PERL51~1.EXE ±w
Print ³enter your name´;
$name=<STDIN>
Print ³Hello, @name´; #interpolation
CHOP:
chop($name);# chop will chop ANY last character at the endWhen user clicks on ENTER , it takes as ³CARRIAGE RETURN´
Hence after you input any data in <STDIN>,It will also take in LAST character as ENTER ( one lone of blank data )SO use CHOP to take only the exact data required
Arithmetic operation :
All input by default is a STRING
µ+¶ always converts any input to NUMERICAL values ALWAYS.
So when you give a string say ³abc´ , its value converts to ZERO , As µ+¶ operator converts to numerical.
34abc+10 = 44 ( as 34 is tak en , after that its ZERO value , till it finds numerical. Abc34 + 10 = 10 ( as abc is alreasy zero , so it stops scanning for numerical.
print "index is $i and value is $myArray[$i] \n";++$i;
}
#FOR EACH loop , mainly to process a LIST, scalar followed by a list
foreach $v (@myArray) #more like foreach $v ( 1,2,3,4,5){
print "Value is $v \n";
}
foreach (@myArray) #this is without a SCALAR variable{
print "Value is without a variable $_ \n";}
#Create array by name arra y1 amd store few city names#defined a foreach loop which update each array element as city_KN
@arraycity=(Delhi,Bombay,Bangalore);
foreach $c (@arraycity)
{print "List of cities after impeding is $c"."_KN"." \n";
}
#!C:/Perl/bin/PERL51~1.EXE -w
#String functions
$str1="bangalore";print "my string is $str1\n"; #to show current string$x=uc($str1);print "x is $x\n"; # this does not update exsisting string , only give new value
#create array by empname , store few emp names.
#define for loop to update each element in below format#uppercase of empname : lengthofempname#RAM:3#RADHA:5
@empName=(Sush,Saha,RAM,DEEpu);print "Names of employess at present @empName \n";
foreach $e (@empName) # takes one string at a time and assigns to scalar
<=>: for numerical comparision : by default shows in Ascending , by ASCII value
If you want STRING in Ascending or Numerical in Decending then ,
@slist=sort {$b cmp $a } (@numlist);
@slist1=sort {$a <=> $b } (@num1);
$b then $a shows Higest to lowest ± Decending order
$a then $b shows lowest to highest ± Ascending order
Reverse ± simply reverse string ( no sorting nothing)
@x=reverse(@num1);
print "after reverse @x\n";
DAY 2 :
PUSH fuction RETURNS the length of the updated array.
@a1=(1,2);print "array is @a1 \n";@b1=push (@a1,3);
print "now array is @b1 \n"; #PUSH fuction RETURNS the length of the updated array.
print "now array is @b1 \n"; #PUSH fuction RETURNS the length of the updated array.
($x,@a2)=@a1; # stores x= first element and @a2 = all other elements in RHS array($x,$y)=@a1 ; # stores x= first element , y = second element(@a3,$z)=@a1; # stores a3 has all array , and z = null
while (<STDIN>)
{chop($_);#If you use CHOMP , error , as it chomps off even ENTER key , not realising its END of array push(@num,$_);
print "@{[%hash3]}"; # to print all elements inside a HASH
SUBROUTINES :
#used to get back a value from called function to calling function Arguments are stored in ³@_´ by default
DISPLAY :May pass the values , or may not pass the values or arguments.
sub display {
print "control is in display function \n";print "display :@_ \n"; # by default , any arguments passed into a function , is stored is an array}
print "control is main routine \n";display;print "control is back to main routine \n";
display(10);display("hello","how");
print "List with array return is @result \n" ; #this takes full array as one big SCALAR , and prints , thatwhy printing HASH is circus with many brackets.
print "Odd number list using PRINT ". AddOdd(@myArray)."\n"; #this concatenation can only PRINTONE SCALAR value , so LENGTH of Odd list is shown
MY and LOCAL variables :
MY is only for a particular blockLOCAL is accessible to all its CHILD blocks too . Scope is for all blocks which calls that function .
BEGIN and END :
BEGIN : BEGIN is inbuild procedure , making variables as STATIC# hence using BEGIN updates the value of x each time after a subroutinue ( so next time you can buildon previous value
END # Need not CALL the function END , it WILL execute anyways if its defined under END{print "End of script \n";}
/abc\s+xyz/ \s+ is once and unlimited\s* will be zero or any number
DAY 3 :
Global Pattern
@a=$mystr=~m/[0-9][0-9] /g;
This makes to check pattern to check in FULL string ,Else simple pattern search , look sonly inside FIRST pattern
#Regular expressions , to chekk which pattern made it MATCH , GLOBAL pattern#GLOBAL pattern searchs ALL occurnces in entire string . else simple checks only for FIRSToccurrence
^ - start with$ - end with+ - min once , max anything³*´ ± zero or max once ( optional ) , it might or might not be there .? ± min zero or max once³. ³- one character \b ± word boundry
\s ± space() ± bound ry\d can be used instead of [0-9]\w can be used instead of valid alphabet , which matches all these : [0 -9][a-z][A-Z] and ³_´underscore.
() boundry ± starts and ends with same , used for BACk reference
Older explanation :
³+previous character, check wether previous occurs miimun once , max ay time ( mandatory )
*Applicable previous, check wheather , check weather previous occurs , min ZERO , max One time (optional)
?applicable , min Zero , max 1 time ( not unlimited )´
() boundry :
Values are stored in buffers names \1 and \2 etc«. to refer it back
print "@a"; # this takes all charcters that is NOT a NUMBER , and "ig" together makes globaland case sensitive
SUBSTITUTION :
$str = "city name is mysore";
$str=~s/my/BANG/; # this replaces "my" with "BANG" and keeps all extra charaters as it is
print "now my str is $str\n";
OUTPUT : city name is BANGsore
$str=" a house is my city ";
# check if multiple spaces are seen , remove and keep only ONE space$str=~s/\s+/ /g; # if you give"\s" it types it , so hust give a SPACE to replace by a spaceprint " now string is:$str\n";
³city name 10 and 200 mysore´
~s/\b(\w+)(e)\b/$2$1/g; # this is searching only for any WORD pattern .as ³\w´ includes anything as 0-9 , a-z,A-Z, and Unsderscore.
~s/\b(\w+)(e)\b/\2\1/g;
Use \1 or $1 , \2 or $2
Above result is making ³nam´ into $1 , then ³e´ into $2,Then pattern in storing ³mysor´ in $1 and ³e´ in $2 .
$1 and $2 stores only the PATTERN and not the actual data .
So Output will be :
City enam 10 and 200 emysore
REFERENCES :
In regular Functions , any value was passed as ONE big ARRAY !!If you need to keeo that structure as it is , use References.
print " first element is $array1[0] \n";print " first element is ${$aref}[0] \n";
print " the arary is : @array1 \n";print " the arary is : @{$aref} \n";
print "Last element is : $#array1 \n";print "Last element is : $#{$aref} \n";
just replace ³arrayname´ with ³$aref´ , as above example.
Use {} flower bracket to make the code more visible and neat
To find LENGTH :
$len1=@array1;print "length is $len1 \n";
give an ARRAY into a SCALAR , it always takes the LENGTH , ( not the first element of thearray into scalar !!!!)
QUOTES :
#%myHash=("apple","red","grapes","green");
%myHash=qw(apple red grapes green); # use "qw" if all are STRINGS , else have to QUOTEeach element manually
#if you need not perform any arithmetic operations then you can QUO TEthat too, such as emp ID
Anonymous ARRAY or HASH :
Usually in big data size, you will any way work only on the data ,So why wate a variable by naming an Array or Hash ,So simply create data directly by using a Reference .
#@a1=(1,3,4); # so you are SAVING a variable a1 , as you are anyways refrening only to thatarray block , and not the NAME!#$ref1=\@a1;
³<´ READ³>´WRITE, erase all exsitng data³>>´ append , add to existing data³+<´ read and write mode , if file exits,then write to it .(cant CREATE a file)³>+´ write plus read mode,if file does not exits,it CREATES file,but OVERWRITTES³>>+´ append and read mode, if file does not exits,it CREATES file,but adds at the LAST .
++++++++++++++++++++++++++++++++++++++++++++++Open(FH,´D:\\flower.txt´) || die ³file cannot open \n´;
While (<FH>)
{print $_;}
Close(FH);++++++++++++++++++++++++++
Open(FH,´<D:\\flower.txt´) || die ³file cannot open \n´;
Above line : All data inside D:\\ drive and flower.txt loads into FH,³<´ reads from the file And remain in that cache till you close FH.
³||´ means OR , so if cannot open,then print .
Print ³Hello´; Actually meansPrint STDOUT ³Hello´;STDOUT throws display to screen ± it¶s a Handler
You can define any File Handler in any name you want .
Like FH , WFH , FH1 , FH2 .. etcIts just a name given by you .
PACKAGES and MODULES :Store it as extension .pm ( means PERL MODULE)
Every package MUST return a TRUE Value ( or 1 ) !!
# PACKAGES AND MODULES#design this below Package/module then use it in any other normal script
++++++++++++++++++ below file name is saved as ³ConverterPackage.pm´
package converter ; # package is a name space
sub uppercase {
my $r=uc($_[0]);print "result is $r \n";
}
sub lowercase {
my $r=lc($_[0]);print "result is $r \n";
}
1; # this is to return a TRUE value
++++++++++++++++++++++++
#Use that Converter Package to work on this script
use ConverterPackage; # File Name with ".pm" extension
converter::uppercase("flower"); # LHS will be that BIG function , also called NAMESPACE, and on RHS is the inner function name.converter::lowercase("FLOwer");
If the Pacxkage file is not in the SAME FOLDER AS YOUR ³.PL´ FILE,then thereis a inbuilt function ³@INC´Gets pathsof where all PERL is present ,So when you say :
USE Converter ,This statement actually invokes @INC ,Which inturns looks for the ³ConveterPackage.pm´ file in all the PATHS definedinside @INC,If It dint find it under any of those , then throws error ,
Telling it dint find the file !
How to Download a module and use it :
Refer : ³CommandpromptCodes.docx´ in the PERL folder that I have saved,it¶s a copy of entire Command prompt screen.
Goto ³cpan.org´Goto PERL Module ->
CPAN search -> look for IxHash (This is used to SORT an array)
Open any of listedDownloadSave to any folder
Extract the zip file
Goto command prompt :
Goto path where you saved that module
Then type in cmd :C:\>xyzWhateverPath>perl Makefile.pl this EXECUTES the PERL fileC:\>xyzWhateverPath>perl Build.pl this EXECUTES the build file
C:\>build test this is to simply check if BUILD has been complied or not !C:\>build install this INSTALLS the build contents
After this , it will show what all Tie-IxHash installs :