What Is Perl?
What Is Perl?
• Though Perl is not officially an acronym but few people used it as Practical
Extraction and Report Language.
• It is used for mission critical projects in the public and private sectors.
• Perl is an Open Source software, licensed under its Artistic License, or the GNU
General Public License (GPL).
• Perl is extensible. There are over 20,000 third party modules available
from the Comprehensive Perl Archive Network (CPAN).
$a = 10; Value of a = 10
print "Value of a = $a\n"; Value of a = $a\n$
print 'Value of a = $a\n';
Data Types
Scalar Scalars are simple variables. They are preceded by a dollar
sign ($). A scalar is either a number, a string, or a reference.
A reference is actually an address of a variable, which we
will see in the upcoming chapters.
Arrays Arrays are ordered lists of scalars that you access with a
numeric index, which starts with 0. They are preceded by an
"at" sign (@).
Hashes Hashes are unordered sets of key/value pairs that you access
using the keys as subscripts. They are preceded by a percent
sign (%).
# This is case of interpolation. Welcome to
$str = "Welcome to \n vishwaniketan!";
print "$str\n"; vishwaniketan!
• OutPut:-
• $ages[0] = 25
• $ages[1] = 30
• $ages[2] = 40
• $names[0] = A
• $names[1] = B
• $names[2] = V
Hash Variables
• %data = ('A B', 45, 'CD', 30, 'PQR', 40);
• print "\$data{'A B'} = $data{'A B'}\n";
• print "\$data{'CD'} = $data{'CD'}\n";
• print "\$data{'PQR'} = $data{'PQR'}\n";
• Output:-
• $data{A B'} = 45
• $data{‘CD'} = 30
• $data{‘PQR'} = 40
Variable Context
• Output:-
• Given names are : A B DC PQR
• Number of names are : 3
Numeric Scalars
• $integer = 200;
• $negative = -300;
• $floating = 200.340;
• $bigfloat = -1.2E-23;
• # 377 octal, same as 255 decimal
• $octal = 0377;
• # FF hex, also 255 decimal
• $hexa = 0xff;
• print "integer = $integer\n";
• print "negative = $negative\n";
• print "floating = $floating\n";
• print "bigfloat = $bigfloat\n";
print "octal = $octal\n";
• print "hexa = $hexa\n";
• Output:-
• integer = 200
• negative = -300
• floating = 200.34
• bigfloat = -1.2e-23
• octal = 255
• hexa = 255
Loop
• for($i=1;$i<10;$i++)
{
printf("Iteration $i\n");
}
• $k=0;
while ($k<10) {
printf("Iteration $k\n");
$k++;
}