You are on page 1of 45

PERL

"Practical Extraction and Reporting


Language"
History
• Perl was created by Larry Wall in 1987
• he was employed on a bug reporting system
and "AWK"- a programming
• Perl in one sentence: Perl is a high-level,
interpreted, dynamic programming language.
• Perl is an interpreted programming language.
Where is Perl used
• Perl is in Web development
• Perl is also used to automate many tasks in
the Web servers
• Perl is still used for its original purpose i.e.
extracting data and generating reports
• It can produce reports on resource use and
check for security issues in a network.
• Apart from all this perl can also be used for
CGI programming.
Example: Perl hello world

#!/usr/bin/perl
print "Hello, world!";
Install Perl
Run Perl

• perl firstprog.pl
• chmod +x firstprog.pl
• ./firstprog
Perl Variable
Scalar Variable
• This type of variable holds a single value.
• Its name begins with a dollar sign and a Perl
identifier (it's the name of our variable).
Correct method
• $var;
• $Var32;
• $vaRRR43;
• $name_underscore_23;
Wrong Method
• mohohoh # $ character is missing

• $ # must be at least one letter

• $47x second character must be a letter

• $variable! # you can't have a ! in a variable name


Numbers:
• In this type of scalar data we could specify:

• integers, simply it's whole numbers, like 2, 0, 534


• floating-point numbers, it's real numbers, like
3.14, 6.74, 0.333

Notice: In general, Perl interpreter sees integers like


floating points numbers.
For example, if you write 2 in your programs, Perl
will see it like 2.0000
Integer literals
• It consists of one or more digits, optionally
preceded by a plus or minus and containing
underscores.
Perl Examples:
• 0;
• -2542;
• 4865415484645
#this also can be written with underscores (for
clarity) :
4_865_415_484_645
Floating-point literals
• 3.14;
• 255.000;
• 3.6e20;
# it's 3.6 times 10 to the 20th
• -3.6e20;
# same as above, but negative
• -3.6e-20;
#it's negative 3.6 times 10 to the -20th
• -3.6E-20;
#we also can use E – this means the same the
lowercase version -3.6e-2
Strings
Like numbers there are two different types of
strings:

• Single quotes string literals


• Double quotes string literals
Single-quoted string literals
• #!/usr/bin/perl
• $num = 7;
• $txt = 'it is $num';
• print $txt;
OUTPUT
it is $num

Here due to single quotes value of $num in not


taken and the literal characters '$','n', 'u' & 'm'
are added to the value of $txt
Double-quoted string literals

•$num = 7;
•$txt = "it is $num";
•print $txt;
OUTPUT
it is 7

• Here due to double quotes value of $num is


taken added to the value of $txt

• Double-quotes interpolate scalar and array


variables, but not hashes. On the other hand,
you can use double-quotes to interpolate
slices of both arrays and hashes.
Construct Description
\n newline
\r return
\t tab
\f formfeed
\b backspace
\a bell
\e escape
\007 any octal ASCII value (here, 007 = bell)
\x7f any hex value (here, 7f = delete)
\\ backslash
\" double quote
\l lowercase next letter
\L lowercase all following letters until \E
\u uppercase next letter
\U uppercase all following letters until \E
\E Terminate \L, \U
String Program
$string = 'tutorial';
# give $string the eight-character string 'tutorial'
print $string;
$string = $size + 3 ;
# give $string the current value of $size plus 3
print $string;
$string = $ string * 5;
#multiplied $string by 5
print $string;
OUTPUT -> tutorial315
String Concatenation (period) :
• The concatenation operator "." unites two or
more strings.
#!/usr/bin/perl
$a = "Tom is";
$b = "favorite cat";
$c = $a ." mother's ". $b;
print $c;
Output:

Tom is mother's favorite cat


Conversion Between Numbers and
Strings:

$string = "43";
$number = 28;
$result = $string + $number;
print $result;
Output:

71

the value of $string is converted to an integer


and added to the value of $number.

The result of the addition, 71, is assigned to


$result.
Scope of a variable – Access Modifiers
There are 3 types of modifiers

•my
•local
•our
My: Using this you can declare any
variable which is specific within the
block. i.e. within the curly braces.
#!/usr/bin/perl
my $var=5;
if(1)
{
my $var_2 =$var;
}
print $var_2;
• No Output

• The output of the program will be nothing!


LOCAL
#!/usr/bin/perl
$var = 5;
{
local $var = 3;
print "local,\$var = $var \n";
}
print "global,\$var = $var \n";
output
• The output of the above program will be in
this manner.

• local, $var = 3

• global, $var = 5
• Our: Once a variable is declared with access
modifier "our" it can be used across the entire
package. Suppose, you have Perl module or a
package test.pm which has a variable declared
with scope our. This variable can be accessed
in any scripts which will use that package.
Perl Array
An Array is a special type of variable which
stores data in the form of a list;
each element can be accessed using the index
number which will be unique for each and
every element.
You can store numbers, strings, floating
values, etc. in your array.
In Perl, you can define an array using '@'
character followed by the name that you want
to give.
Declare Array
my @array=(a,b,c,d);
print @array;
You can also declare an array in the above way;
the only difference is, it stores data into an array
considering a white space to be the delimiter.
Here, qw() means quote word.
@array1=qw/a b c d/;
@array2= qw' p q r s';
@array3=qw { v x y z};
print @array1;
print @array2;
print @array3;
Value Assignment
Suppose you want to assign a value to the 5th
element of an array, how are we going to do
that.

$array [4] ='e';


Sequential Array

Sequential arrays are those where you store


data sequentially
@numbers= (1..10);
print @numbers;
#Prints numbers from 1 to 10;

Output: 12345678910
Perl Array Size

@array= qw/a b c d e/;


print $size=scalar (@array);
print "\n";
print $size=$#array + 1;
# $#array will print the Max Index of the array,
which is 5 in this case
Output : 5 5
Dynamic Array
Dynamic arrays are those that you declare without
specifying any value on them.
my $string="This is a kind of dynamic array";
my @array;
@array=split('a',$string);
foreach(@array)
{
print "$_ \n”;
# This is a special variable which stores the current
value.
}
output
This is

kind of dyn

mic

rr

y
Push, Pop, shift, unshift for Perl
arrays:
These functions can be used in Perl to add/delete to
array elements.
 Perl Push: adds array element at the end of an
existing array.
 Perl Pop: removes the last element from an array.
 Perl Shift: removes the first element from an array.
 Perl Unshift: adds an element at the beginning of
an array.
@days = ("Mon","Tue","Wed");
print "1st : @days\n";
push(@days, "Thu");
print "2nd when push : @days\n";
unshift(@days, "Fri");
print "3rd when unshift : @days\n";
pop(@days);
print "4th when pop : @days\n";
shift(@days);
print "5th when shift : @days\n";
OUTPUT

1st : Mon Tue Wed

2nd when push : Mon Tue Wed Thu

3rd when unshift : Fri Mon Tue Wed Thu

4th when pop : Fri Mon Tue Wed

5th when shift : Mon Tue Wed

You might also like