Thursday, April 24, 2014

PERL Training

PERL TRAINING
---------------------------------------------
Day1: Data Structures and Inbuilt functions
Day2: Complex data structures & Perl modules
Day3: Object oriented Perl & DBI, CGI, Network, Process, Threads

*******************
DAY 1
*******************
PERL: Practical extraction and reporing language

Most of the features are derived from C, Shell, awk, sed and C++

--> perl -v ------------------> Shows the version of the perl which is installed
--> perl -V -----------------> Shows the location of the perl libraries

----------------------------------------------
Scalar variable:

Prefix of the variable will denote the type of the variable.
ex: $a ---> its a scalar variable
$name= 'arun';
$value=2.5;
**Every variable in PERL is initialized.
**By defalut the variable is initialized to "UNDEF". undef does not has any type, that is neither string, or int or float.
RESETING A VARIABLE
$a=10;
$a=undef;
or
undef($a);

**Same variable name can be used to denote different datatypes
Ex: $a ---------> scalar variable
@a ---------> array
%a ---------> Hash

**User defined variables start with alphabets;
**inbuilt variables will start with num/special character
==================================================
Program to read the input from user:
==================================================
#! /usr/bin/perl

package main;

$a=<STDIN>;
$b=<STDIN>;

$res=$a+$b;
print "' Result = $res";

======================================================================== Program to print the environment variable or shell variable in PERL Program
========================================================================
#!/usr/bin/perl

package main;

print "Printing environment variable";

print "value = $ENV{PATH}";

OUTPUT:

[ngdbprod@ngdb Vishwa_Perl]$ perl 3.pl
Printing environment variablevalue = /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin:/Softwares/ant/apache-ant-1.8.2/bin:/opt/svn/bin:/opt/maven/apache-maven-3.0.4/bin:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/ngdbprod/bin[ngdbprod@ngdb Vishwa_Perl]$

======================================================================== PROGRAM TO PRINT THE VARIABLE IN BINARY VARIABLE (using type specifier ex:%d, %b, %f etc)
========================================================================
#! /usr/bin/perl

package main;

$a=20;
print "TO PRINT THE VARIABLE IN BINARY\n";
printf("' Result %b",  $a);

OUTPUT:
[ngdbprod@ngdb Vishwa_Perl]$ perl 4.pl
TO PRINT THE VARIABLE IN BINARY
' Result 10100
[ngdbprod@ngdb Vishwa_Perl]$
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
=====================================================================================================
PROGRAM TO PRINT THE VARIABLE IN BINARY VARIABLE (using type specifier ex:%d, %b, %f etc)
=====================================================================================================



@@@@@@@@@@@@@@@@@@@@@@@@@
PERL DOCUMENTATION
@@@@@@@@@@@@@@@@@@@@@@@@@

**Its help on any inbuilt functions
**Perl documentation is the utility available in PERL to understand the documentation of the default inbuilt functions available in PERL

ex:    perldoc -f <function name>
perldoc -f print
perldoc -f chomp

**To get the help on the inbuilt variables
ex:    perldoc -v $,;
perldoc -v $a

**To recursive search for a word in PERL DOCUMENTATION
ex:   perldoc -r "word"

**To Query the perl FAQs
ex:   perldoc -q date
       perldoc -q socket

** To get the help on perl library
ex:    perldoc List::<Library>
perldoc List::Util

SYNOPSIS
           use List::Util qw(first max maxstr min minstr reduce shuffle sum);

EX:
Its similar to #include of the C programming
#include<math.h>
use List::Util qw(sum)
================================================================================
PROGRAM TO USE PERL LIBRARY UTILITIES
================================================================================
#! /usr/bin/perl

package main;

use List::Util qw(sum);

print sum(10,20.30);
print"\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 5.pl
30.3
[ngdbprod@ngdb Vishwa_Perl]$
================================================================================
PROGRAM TO USE MULTIPLE PERL LIBRARY UTILITIES IN A SINGLE PERL PROGRAM
================================================================================
#! /usr/bin/perl

package main;

use List::Util qw(sum max);

print sum(10,20,30);
print"\n";

print max(10,20,40),"\n";

[ngdbprod@ngdb Vishwa_Perl]$

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 5.pl
60
40
[ngdbprod@ngdb Vishwa_Perl]$

** TO LIST ALL THE PERLDOC FOR THE INBUILT FUNCTIONS
ex:
perldoc perlfunc
** TO LIST ALL THE PERLDOC FOR THE INBUILT VARIABLES
ex:
perldoc perlvar
** TO LIST ALL THE PERLDOC FOR THE INBUILT OPERATORS
ex:
perldoc perlop
** TO LIST ALL THE PERLDOC MAJOR FEATURES
ex:
perldoc perlcheat
** TO LIST ALL THE PERLDOC FOR THE INBUILT TUTORIALS
ex:
perldoc perl


################################
SCALAR INBUILT FUNCTIONS
################################

$name='arun';
$res=uc($name);  -->converts to uppercase
$res=lc($name); -->Converts to lowercase
$n=length($name); -->Length of the string
$n=index($name,'u');         -->Search for the index of the char in the word
$n=rindex($name,'u'); -->Search for hte index of the char in the word from the rightside
$res=substr($name,0,3);   -->Return the characters from 0 till 3rd char
$res=reverse($name); -->Reverse of the name

==================================================
PROGRAM
==================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 6.pl
$name='arun';
        $res=uc($name);
print"$res\n";
        $res=lc($name);
print"$res\n";
        $n=length($name);
print"$n\n";
        $n=index($name,'u');
print"$n\n";
        $n=rindex($name,'u');
print"$n\n";
        $res=substr($name,0,3);
print"$res\n";
        $res=reverse($name);
print"$res\n";
[ngdbprod@ngdb Vishwa_Perl]$

OUTPUT

[ngdbprod@ngdb Vishwa_Perl]$ perl 6.pl
ARUN
arun
4
2
2
aru
nura
[ngdbprod@ngdb Vishwa_Perl]$


================================================================
CHOMP FUNCTION
chomp is a special function which deletes the trailing new line character from the input
==================================================================

[ngdbprod@ngdb Vishwa_Perl]$ cat 7.pl
chomp($str1=<STDIN>);
chomp($str2=<STDIN>);
$str3=$str1.$str2;

print "Concatinated String is $str3";

OUTPUT
hari
prasad
Concatinated String is hari prasad

[ngdbprod@ngdb Vishwa_Perl]$

========================================================================
PROGRAM TO DEMO STRING MANUPULATION
========================================================================

[ngdbprod@ngdb Vishwa_Perl]$ cat 8.pl
chomp($str1=<STDIN>);
$res1=substr($str1,0,3);
print "FIRST FOUR CHARACTER OF THE STRING $res1\n";
$res2=uc(substr($str1,0,3));
print "FIRST FOUR CHARACTER OF THE STRING CONVERTED TO UPPERCASE $res2\n";
$res3=substr($str1,-3);
print "Last three character of the string $res3\n";
$res4=reverse(substr($str1,-3));
print "Last three character of the string in reverse order $res4\n";
$res5=substr($str1,4,-3);
print "Extract except first 4 and except last 3 and store in avariable $res5 \n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 8.pl
hellowworld
FIRST FOUR CHARACTER OF THE STRING hel
FIRST FOUR CHARACTER OF THE STRING CONVERTED TO UPPERCASE HEL
Last three character of the string rld
Last three character of the string in reverse order dlr
Extract except first 4 and except last 3 and store in avariable owwo
[ngdbprod@ngdb Vishwa_Perl]$

========================================================================
PROGRAM TO DEMO STRING MANUPULATION
========================================================================
chomp($str1=<STDIN>);
$n=index($str1,a);
substr($str1,$n,1,'1');
print "REPLACE THE FIRST OCCURANCE OF THE a with 1 $str1\n";
$n=rindex($str1,a);
substr($str1,$n,1,'2');
print "REPLACE THE LAST OCCURANCE OF THE a with 1 $str1\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 9.pl
vishwanath
REPLACE THE FIRST OCCURANCE OF THE a with 1 vishw1nath
REPLACE THE LAST OCCURANCE OF THE a with 1 vishw1n2th
[ngdbprod@ngdb Vishwa_Perl]$

========================================================================&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&COMPARISON OPERATOR
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


1. Numeric comparison --> '=='
2. String Comparison  --> 'eq'

Numeric Operators:
==                  --> eq
!=                   --> Not equal to
>                    --> Greater than
<                    --> Less than
>=                  --> Greater than or equal to
<=                  --> Smaller than or equal to


&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&Branching Constructs
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


if statement
------------------------
if(expr)
{
statement;
}
else
{
statement;
}


if else if statement
------------------------
if(expr)
{
statement;
}
elseif(expr)
{
statement;
}
else
{
statement;
}

negated if statement
------------------------
if(!expr)
{
statement;
}
else
{
statement;
}

OR

unless(expr)
{
statement;
}
else
{
statement;
}

========================================================================PROGRAM TO DEMONSTRATE IF else
program accepts two numbers and checks whether the first digit of first
number and last digit of the second number are same
========================================================================


[ngdbprod@ngdb Vishwa_Perl]$ cat 11.pl
#! /usr/bin/perl
print "Enter a number\n";
chomp($str1=<STDIN>);
print "Enter another number\n";
chomp($str4=<STDIN>);
#$str2=substr($str1,0,1);
#$str3=substr($str4,-1);
#if($strr2 eq $str3)


if(substr($str1,0,1) eq substr($str4,-1))
{
print "First and last digits are same\n";
}
else
{
print "Digits are not same\n";
}
[ngdbprod@ngdb Vishwa_Perl]$
OUTPUT
Enter a number
12342
Enter another number
56437681
First and last digits are same

========================================================================PROGRAM TO CHECK WHETHER A STRING IS PALINDROME OR NOT Case insensitive
========================================================================


[ngdbprod@ngdb Vishwa_Perl]$ cat 10.pl
#! /usr/bin/perl
chomp($str1=<STDIN>);
$str1=uc($str1);
print "Upper case:$str1\n";
$str2=reverse($str1);
if($str1 eq $str2)
{
print "String is a palindrome\n";
}
OUTPUT
Nitin
Upper case:NITIN
String is a palindrome

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SWITCH STATEMENT
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

                IN C IN PERL
                 ----               -------
           switch(expr)                     given(expr)
             {         {
               case 1: when(1)
               case 2: when(2)
            }done         default
                }


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LOOPS IN PERL
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*

**for
**while
**dowhile
**until
**do until
**foreach

=======================================================
PROGRAM TO DEMO WHILE LOOP
Accept a string and print it vertically
=======================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 12.pl
chomp($str1=<STDIN>);
#$i=1;
while($a=substr($str1,$i,1))
{
print "$a\n";
$i++;
}
[ngdbprod@ngdb Vishwa_Perl]$ perl 12.pl
vishwanath
v
i
s
h
w
a
n
a
t
h

==================================================================
FOREACH LOOP
==================================================================
$a=10;
$b=20;
$c=30;
foreach $i ($a,$b,$c)
{
$i--;
}
print "a = $a\n";
print "b = $b\n";
print "c = $c\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 13.pl
a = 9
b = 19
c = 29

========================================================================
MISSING ALIAS In FOREACH LOOP
Here in this, instead of specifying "$i" after foreach, nothing specified.
Instead we get "$_" as default alias
========================================================================
$a=10;
$b=20;
$c=30;
foreach ($a,$b,$c)
{
$_--;
}
print "a = $a\n";
print "b = $b\n";
print "c = $c\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 13.pl
a = 9
b = 19
c = 29

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@LISTS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


A series of variables seperated by commas and enclosed in braces is LIST

**Assignment
----------
($a,$b)=(10,20);

**Swapping the values of variables
--------------------------------
($a,$b)=($b,$a);

**LIST indexing
----------------
($a,$b)=(10,20,30,40)[-1,2]
This will assign 40 and 30 to $a and $b respectively.
Quoted words(qw)
--------------------
('max','min')
OR
qw(max,min)

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ARRAYS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

**How to declare a empty array
my @arr;

**Define a array with values
my @arr=(1,2,3,4,5);

**How to pick the first element
print "$a[0]"

**Print Last Element of array
print "$a[-1]"

**First 3 elements of array
print "@arr[0,1,2]"

**Print Complete array
print "@arr"

**Print the length of array
1. $n=$#arr+1
2. $n=scalar @arr
3. $n=@arr

========================================================================PROGRAM TO DEMO ARRAY OPERATIONS
========================================================================

print "Extract the first 5 elements of the array and store in array\n";
@arr=(10,20,30,40,50,60,70,80,90,100);
@a=@arr[0..5];
print "the first five elements are @a\n";

print " Increment the first five elements\n";
$i=0;
while($i<5)
{
$a[$i]=$a[$i]+1;
$i++;
}
OR
foreach (@a)
{
$_++;
}
print " Incremented the first five elements by one @a\n"

OUTPUT:
[ngdbprod@ngdb Vishwa_Perl]$ perl 14.pl
Extract the first 5 elements of the array and store in array
Contents of the array before manupulation 10 20 30 40 50 60 70 80 90 100
the first five elements are 10 20 30 40 50 60
Increment the first five elements
Incremented the first five elements by one 11 21 31 41 51 60

========================================================================
PROGRAM TO DEMO ARRAY OPERATIONS
Here, program accepts 5 user inputs into a empty array, and by
using inbuilt function of the PERL, prints the Largest,
smallest and sum of all the elements
========================================================================
[ngdbprod@ngdb Vishwa_Perl]$ cat 15.pl
use List::Util qw(max min sum);
print "Declare a empty array with name as nums\n";
my @nums;
foreach (0..4)
{
chomp($temp=<STDIN>);
push(@nums,$temp);
$_++;
}

print "Biggest element of the array\n", max(@nums);
print "\nSmallest element of array\n", min(@nums);
print "\nSum of all the elements\n", sum(@nums);

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 15.pl
Declare a empty array with name as nums
45
2
87
32
89
Biggest element of the array
89
Smallest element of array
2
Sum of all the elements
255

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
LIST OF INBUILD FUNCTIONS FOR ARRAYS
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


push(@arr, list)         --> Added at the end
pop(@arr) --> Deleted element at the end
unshift(@arr,list)         --> Added at the START
shift(@arr) --> Delete one element from the start
reverse(@arr)
sort(@arr)
grep
map
join
split


**sort(@arr
------------
sort by default sorts the string into ascending order and in ASCII order
syntax:
          @names= sort qw(................);


For sorting numbers
@nums = sort {$a <=> $b } @nums; where $a & $b are inbuilt variables

%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f sort %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


ex:
@name = qw (vish, naga, anil, mukta)

print join("\n",@names);
#sort the array
@names=sort{@a cmp @b} @names;

#Sort the array based on length of items of array
@names =  sort{ length($a) <=> length($b)} @names

==============================================================
How to print the elements of a array in new lines
==============================================================

@names = qw(vish naga anil mukta);

print join("\n",@names);
#sort the array
@names=sort{@a cmp @b} @names;
print "\n@names\n";
#Sort the array based on length of items of array
@names =  sort{ length($a) <=> length($b)} @names;
print "\n@names\n";

#sort them based on last char
@names = sort{ substr($a, -1) cmp substr($b, -1)} @names;
print "\n@names\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 16.pl
vish
naga
anil
mukta
vish naga anil mukta

vish naga anil mukta

naga mukta vish anil
[ngdbprod@ngdb Vishwa_Perl]$

@@@@@@@@@@@@@@@@@@@@@@@@@@@
GREP
@@@@@@@@@@@@@@@@@@@@@@@@@@@
Syntax
@res= grep(/regex/,@arr);

Compares the regex accross each element of the array and returns the matching elements
===================================================================
#Display only those dates which fall in the current month
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f localtime %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
THe program uses perl function "localtime" which returns a list containing the details of current date.
The month is returned as a number where 0 denotes jan and 11 dec.
===================================================================

@arr=qw(15-4 10-2 11-4 17-4 23-5 24-6 30-7 12-3 29-3);
@localTime = localtime;
$mon=@localTime[4];
print "MONTH--> $mon\n";
@res = grep(/-$mon/,@arr);
print " Array elements in the current month @res\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 17.pl
MONTH--> 3
Array elements in the current month 12-3 29-3
[ngdbprod@ngdb Vishwa_Perl]$

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
MAP
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IF any operatin needs to be performed on all the elements then use maps

Ex: Increment all the elements by 1
@arr=(10,20,30,50,60,90)
map($_++,@arr)

@names=qw(arun ram,rama,sita);
#store teh first char of the each elemet in @temp;
@temp=map(substr($_,0,1),@names);

Convert all the elemets of array to UPPER CASE
@names =map(uc,@names);

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SPLIT
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


Syntax
@res=split(/regex/,SCALAR);

To get the last word of the below sentence
$str='end of the program';
@res = split(/ /, $str)
$word = @res[3];

**If there are multiple delimeters in a sentence then we have to use sqare brackets
ex:
split(/[,:; \-]/, str);
** IF A deleimeter occurs more than once
ex:
if there are more than one space in a sentence
split(/ +/, @str);
**IF deleimeter is NULL, split splits the sentence character by character including space
$str='is of';
@res=split(//,$str);
$n=@res;
print "$n";

**IF deleimeter is / */, split splits the sentence character by character including space
$str='is of';
@res=split(//,$str);
$n=@res;
print "$n";

First Char of word
@res = split(/ /,@str)
@res = map($_, @res)
==================================================================================
#PROGRAM TO PRINT THE FIRST CHAR OF THE EVERY WORD IN SENTENCE ON A NEW LINE
==================================================================================
$str='end of the program';
@res = split(/ /, $str);
@res = map(substr($_,0,1), @res);
print join("\n", @res);

OUTPUT

[ngdbprod@ngdb Vishwa_Perl]$ perl 18.pl
e
o
t
p

@sales = qw(north-45 south-65 east-76 west-87)
@sum = split(/-/,@sales);
foreach(@sum){
$res+=$sum[i];
}

==================================================================================
#PROGRAM TO PRINT THE SUM of the numbers in the array (north-45 south-65 east-76 west-87);
==================================================================================


use List::Util qw(sum);

@sales = qw(north-45 south-65 east-76 west-87);
print "SALES-->@sales\n";

foreach (@sales){
@sum = split(/-/, $sales[$i]);
print "SUM-->@sum\n";
$i++;
$res=sum($res,$sum[1]);
}
print "$res\n";

OUTPUT
[ngdbprod@ngdb Vishwa_Perl]$ perl 19.pl
SALES-->north-45 south-65 east-76 west-87
SUM-->north 45
SUM-->south 65
SUM-->east 76
SUM-->west 87
273

===================================================================================
PROGRAM TO PARSE THE TEXT FILE, the program takes the file name as argument
===================================================================================

INPUTFILE:
 cat data.log
10001 name1 place1 15000,2000,500
10002 name2 palce2 12000,1000,4
10003 name3 palce3 12345,0000,1
10004 name4 palce4 12312,-1,0011


Program:
use List::Util qw(sum);

@arr = <>;
foreach (@arr) {
@fileSplit=split(/ /,$_);
print "After first split\n \t\t@fileSplit\n";
foreach (@fileSplit){
@salary=split(/,/,$fileSplit[3]);
$totalSalary = sum(@salary);
#$totalSalary = sum(salary[1] salary[2] salary[3]);
}
print "Total Salary -->\n \t\t$totalSalary\n";
}

OUTPUT
After first split
10001 name1 place1 15000,2000,500

Total Salary -->
17500
After first split
10002 name2 palce2 12000,1000,4

Total Salary -->
13004
After first split
10003 name3 palce3 12345,0000,1

Total Salary -->
12346
After first split
10004 name4 palce4 12312,-1,0011

Total Salary -->
12322
[ngdbprod@ngdb Vishwa_Perl]$ cat 20.pl

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Traversing the array
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
method1:


**SPecial OPERATORS IN PERL
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f operators
IN Perl 5.10 above, special operators inducted for traversing
================================================================
TO COMPARE TWO ARRAYS
================================================================
@a=(10,20,30);
@b=(50,60,70);
if(@a~~@b)
{
print "EQUAL";
}
else
{
print "NOT EQUAL";
}

FOR SPECIAL MATHEMATICAL OPERATORS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc  Algorithm::Diff %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

LCS = Intersection
DIFF= Minus


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
HASHES
Its a two column table, where first
column contains the key ans second column contains the value
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

scalar
  |
 LIST
  /\
 /  \
/    \
 Array   Hash-Unordered List
Ordered List

** DECLARE A EMPTY HASH
my %hash
** DECLARE A HASH WITH ITEMS
%hash = (a,b,c,d,r,f,g,h);

**IN HASH, all the elements are stored as key value pair, one key can have only one value

**How to get the value of a key in HASH
$value = $hash{key};
** SPECIFIYING MULTIPLE KEYS
@list = @hash{key1,key2,key3};
** HOW TO GET COMPLETE HASH
print "%hash";



Inbuilt Functions in HASH
-delete
$hash{arun}--> This will delete the key value pair of 'arun'
-exists
$hash{hari}--> Returns 1 if that key exists in HASH
Returns undef if not found
-keys
-values
-each
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -q duplicate %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


** how to traverse the hash
method 1:
while(($a, $b)=each(%has))
{
print "$a $b\n";
}
method 2:
foreach $i(keys(%hash))
{
print $i $hash{$i}\n;
}

**SORT HASH based on keys
@temp=sort keys(%hash)
** SORT HASH BASED ON VALUES
@temp=sort{$hash{$b}<=>$hash{$a}} keys(%hash)

INPUT:
$str='two three five one nine';

Output
23519
SOLUTION:
%hash=qw(zero 0 one 1 two 2 three 3 four 4 five 5 six 6 seven 7 eight 8 nine 9);

print @hash{split(/ /,$str};


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
COMPLEX DATA STRUCTURES
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

How to merge the multiple arrays into one?
**@arun=(10,20,30);
@hari=(23,43,65);
@chet=(54,65,76,90);

@merged_Array=(@arun,@hari,@chet);

**HOW To STORE THE REFERENCES OF ARRAYS:

@merged_Array=(\@arun,\@hari,\@chet);

**TO ACCESS THE INDIVIDAUL ELEMENTS:
$arr[0]->[0] OR $arr[0][0] --> This will give first element of array arun i.e.10

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Anonymous Array
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@arr = (10,20,30,40)---> THis is named array
$ref=[10,20,30,40]; ---> This is anonymous array
print "$ref->[0]";--------> THis will print first element of array
print "$ref"; -------------> This will print address array
print @$ref    -------->This will print complete array

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Anonymous hash
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


@arr = {arun=>10,shyam=>20,mina=>30,tina=>40}
$arr = {arun=>10,shyam=>20,mina=>30,tina=>40}  Anonymous Hash
print "$ref"       Address of hash
print "$ref->{arun}"; value of Arun
print %$ref; WHole hash is printed



$testCase1={functionname=fn1,actualResult=undef,expectedResult=10}
$testCase2={functionname=fn2,actualResult=undef,expectedResult=10}
$testCase3={functionname=fn3,actualResult=undef,expectedResult=10}
$testCase4={functionname=fn4,actualResult=undef,expectedResult=10}
$testCase5={functionname=fn5,actualResult=undef,expectedResult=10}

Call the testcases by below line
$red=[$tc1,$tc2...........]

foreach $i (@$ref)
{
$res=&$i->{name}
}










@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
DEBUGGING THE DATASTRUCTURES WITH "DUMPER"
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

sytax
use Data::Dumper

$ref=..................

print Dumper($ref);


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
REGULAR EXPRESSION
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
SPECIAL METACHARS

%%%%%%%%%%%%%%%%%%%%% perldoc perlcheat %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
REGEX METACHARS            REGEX MODIFIERS
          ^     string begin         /i case insens.
          $     str. end (before \n) /m line based ^$
          +     one or more          /s . includes \n
          *     zero or more         /x ign. wh.space
          ?     zero or one          /g global
          {3,7} repeat in range
          ()    capture          REGEX CHARCLASSES
          (?:)  no capture       .  == [^\n]
          []    character class  \s == [\x20\f\t\r\n]
          â     alternation      \w == [A-Za-z0-9_]
          \b    word boundary    \d == [0-9]
          \z    string end       \S, \W and \D negate


$str='varun';
if($str=~/arun/)--------> Does the str contain arun ------> YES TRUE
if($str=~/^a/)--------------------> DOES THE STRING BEGIN WITH a? ------> NO FALSE
if($str=~/arun$/)------------> DOES THE STRING END WITH arun?----------->YES TRUE

[0-9] OR \d
[a-zA-Z0-9] OR \w
[ \t\n] OR \s

[!0-9] OR \D
[!a-zA-Z0-9] OR \W
[! \t\n] OR \s

DAY3

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Subroutines
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

**Subroutines are written before they are called
** Arguments passed to the subroutine are copied into a special array named as "@_"
Syntax:
sub add
{
$res=$_[0] + $_[1];
return $res;
}

add(arg1, arg2)


Whenever a array or hash been passed as a argument, then the reference of the array is to be passed as an argument.
Ex:
@a=(5,6,7);
@b=(1,2,3);
sub func_Call
{
my ($p, $q)=@_;
print "$p->[0]";-----------> Will print the first element of the first array
print "$q->[0]";-----------> Will print the first element of the second
array
}
func_Call(\@a, \@b)


**Lexically scoped variables

Variables declared witht he keyword "my" within subroutine, such variables are termed lexical variable.
"our" variables are global variables.

**NOTE Don't declare any "our" variable withing subroutine.
** Is it compu;lsory to declare the variables in PERL?
Ans: NO,
But when program has

use Strict;
use Warnings;

Then it is mandatory to declare the variables. We have to specify whether that is local variable of global variable.

Package varibles:
The variales which are defined outside a module are called package variables. There scope is accross the package.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
PROGRAM TO DEMO PACKAGES
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
**Below is a package, it is saved as .pm file

[ngdbprod@ngdb Vishwa_Perl]$ cat mylib.pm
package mylib;

sub func1
{
        print "Hello\n";
}
1;
It should always end with 1, so it returns true always

** In the program below, I am including the package mylib by keyword "use".
And used scope resolution operator "::"

[ngdbprod@ngdb Vishwa_Perl]$ cat 24.pl
use mylib;

mylib::func1();
[ngdbprod@ngdb Vishwa_Perl]$


**Every package in PERL has two functions by default. These are called Package constructor and Package destructor.
sub BEGIN,
{

}
sub END
{

}

TO ADD our libraries to the Compilation,
include the below line
 use lib ("path to the library");

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -q @INC %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


Exporting and Importing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc Exporter %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


NAME
       Exporter - Implements default import method for modules

SYNOPSIS
       In module YourModule.pm:

         package YourModule;
         require Exporter;
         @ISA = qw(Exporter);
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       or

         package YourModule;
         use Exporter âimportâ; # gives you Exporterâs import() method directly
         @EXPORT_OK = qw(munge frobnicate);  # symbols to export on request

       In other files which wish to use YourModule:

         use ModuleName qw(frobnicate);      # import listed symbols
         frobnicate ($left, $right)          # calls YourModule::frobnicate

DESCRIPTION
       The Exporter module implements an "import" method which allows a module to export functions and variables to its usersâ namespaces. Many modules
       use Exporter rather than implementing their own "import" method because Exporter provides a highly flexible interface, with an implementation opti-
       mised for the common case.

       Perl automatically calls the "import" method when processing a "use" statement for a module. Modules and "use" are documented in perlfunc and
       perlmod. Understanding the concept of modules and how the "use" statement operates is important to understanding the Exporter.

       How to Export

       The arrays @EXPORT and @EXPORT_OK in a module hold lists of symbols that are going to be exported into the users name space by default, or which
       they can request to be exported, respectively.  The symbols can represent functions, scalars, arrays, hashes, or typeglobs.  The symbols must be
       given by full name with the exception that the ampersand in front of a function is optional, e.g.

           @EXPORT    = qw(afunc $scalar @array);   # afunc is a function
           @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc

       If you are only exporting function names it is recommended to omit the ampersand, as the implementation is faster this way.

       Selecting What To Export

       Do not export method names!

       Do not export anything else by default without a good reason!

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
How to install a module
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

  ** Needs to be searched for the module in "cpan.org"
Remember the path.
Then execute on the local prompt
1: cpan --> THis will return the CPAN prompt
2: install tree::Binary
3: quit
4: perldoc tree::Binary--> If installed successfully, it should show the documentation

You should be a admin or a sudo user


** How to list all the modules?
>> instmodsh
** How will I know whether the module is installed or not
>> perldoc moduleName


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc SelfLoader %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc AutoLoader %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OBJECT ORIENTED PERL
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc -f bless %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc UNIVERSAL %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% perldoc DBI %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

##################################################################################
PROGRAM TO DEMO DATABASE ACTIVITIES IN PERL
#####################################################################################
use DBI;
$dbh = DBI->connect('dbi:odbc:mydriver');
$stmt = $dbh->prepare("select * from emps");
$stmt->execute();

while (@arr=$stmt->fetchrow_array())
{
print "@arr";
}
$dbh->disconnect();

**HOW to execute a shell command within a PERL Program

method1:
system("command");

method2:
@result=`command`;
OR
@result=qx(command); -----------> qx -->Quoted execution

method3:---------> Object oriented way of using method1 and method2
use ARGV;
$r1 = Argv->new;
$r1->exec;



#####################################################################################
IPC
#####################################################################################
FH--> File Handler

open(FH,"dir |");
print <FH>;

close FH;

OUTPUT: LISTS the content of the directory
[ngdbprod@ngdb ~]$ perl 25.pl
25.pl
ambari-0.9
ambari-1.2.0-incubating
ambari-1.2.1-incubating
ambari-1.2.3-incubating
ambari-1.2.3-incubating.tar.gz
Build
fuct.sh
hive-0.11.0-shark-SNAPSHOT-bin.tar.gz
hive-0.11.0-shark-SNAPSHOT-bin.tar.gz.md5
NGDB_RPMS
NGDB_SVN_CODE
NSN-NGDB-POSTGRES-0.1.0-9.1.4.noarch.rpm
shark-0.8.0-bin.tar.gz
sonar-3.3.1
sonar-3.3.1.zip
spark-0.8.0-incubating-bin.tar.gz
Vishwa_Perl
[ngdbprod@ngdb ~]$

No comments:

Post a Comment