Practical Extraction and Reporting Language
Practical Extraction and Reporting Language
#!/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
•$num = 7;
•$txt = "it is $num";
•print $txt;
OUTPUT
it is 7
$string = "43";
$number = 28;
$result = $string + $number;
print $result;
Output:
71
•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
• 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.
Output: 12345678910
Perl Array Size
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