Perl
Perl
INTRODUCTION:
• Perl is a general-purpose programming language originally
developed for text manipulation and now used for a wide
range of tasks including system administration, web
development, network programming, GUI development, and
more.
• It stands for Practical Extraction and Report Language.
• It runs on a variety of platforms, such as Windows, Mac OS,
and the various versions of UNIX.
Perl - Syntax Overview
• A Perl program consists of a sequence of declarations and
statements, which run from the top to the bottom.
• Loops, subroutines, and other control structures allow you to
jump around within the code.
• Every simple statement must end with a semicolon (;).
Interactive Mode Programming
#!/usr/bin/perl
# This will print "Hello, World"
print "Hello, world\n";
• Here /usr/bin/perl is actual the perl interpreter binary.
• Before you execute your script, be sure to change the mode of the script file
and give execution priviledge, generally a setting of 0755 works perfectly
$chmod 0755 hello.pl
$./hello.pl
• parentheses for functions arguments can be usedor omit them according to your personal
taste.
print("Hello, world\n");
print "Hello, world\n";
Perl File Extension
• A Perl script can be created inside of any normal simple-text editor program.
• a Perl file must be saved with a .pl or .PL file extension in order to be recognized as a
functioning Perl script.
• File names can contain numbers, symbols, and letters but must not contain a space.
• Use an underscore (_) in places of spaces.
Comments in Perl
• A line starting with hash # is a comment in perl.
• Lines starting with =begin are interpreted as the start of a section of embedded
documentation (pod), and all subsequent lines until the next =cut are ignored by
the compiler.
#!/usr/bin/perl
# This is a single line comment
print "Hello, world\n";
=begin comment
This is all part of multiline comment. You can use as
many lines as you like These comments will be ignored
by the compiler until the next =cut is encountered.
=cut
Whitespaces in Perl
• A Perl program does not care about whitespaces
#!/usr/bin/perl
print "Hello, world\n";
• But if spaces are inside the quoted strings, then they would be printed as is.
#!/usr/bin/perl
# This would print with a line break in the middle
print "Hello
world\n";
Single and Double Quotes in Perl
• Only double quotes interpolate variables and special characters such as
newlines \n, whereas single quote does not interpolate any variable or special
character.
#!/usr/bin/perl
print "Hello, world\n";
print 'Hello, world\n';
#!/usr/bin/perl $a = 10;
print "Value of a = $a\n";
print 'Value of a = $a\n';
Escaping Characters
• Perl uses the backslash (\) character to escape any type of character that might
interfere with our code.
#!/usr/bin/perl
$result = "This is \"number\"";
print "$result\n";
print "\$result\n";
Perl Identifiers
• A Perl identifier is a name used to identify a variable, function, class, module, or
other object.
• Perl does not allow punctuation characters such as @, $, and % within identifiers.
Sr.No
Types & Description
.
Scalar
1 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
2 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
3 Hashes are unordered sets of key/value pairs that you access using the
keys as subscripts. They are preceded by a percent sign (%).
Numeric Literals
• Perl stores all the numbers internally as either signed integers or double-
precision floating-point values.
• Numeric literals are specified in any of the following floating-point or integer
formats −
Type Value
Integer 1234
Negative integer -100
Floating point 20.00
Scientific notation 16.12E14
Hexadecimal 0xffff
Octal 0577
String Literals
#!/usr/bin/perl
$age = 25; # An integer assignment
$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Array Variables
• An array is a variable that stores an ordered list of scalar values.
• Array variables are preceded by an "at" (@) sign.
• To refer to a single element of an array, you will use the dollar sign ($) with the
variable name followed by the index of the element in square brackets.
Hash Variables
• A hash is a set of key/value pairs. Hash variables are preceded by a percent (%)
sign.
• To refer to a single element of a hash, you will use the hash variable name followed
by the "key" associated with the value in curly brackets.
#!/usr/bin/perl
%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
#!/usr/bin/perl
@names = ('John Paul', 'Lisa', 'Kumar’);
@copy = @names;
$size = @names;
print "Given names are : @copy\n";
print "Number of names are : $size\n";
Sr.No. Context & Description
Scalar
1 Assignment to a scalar variable evaluates the right-hand side in a scalar
context.
List
2 Assignment to an array or a hash evaluates the right-hand side in a list
context.
Boolean
3 Boolean context is simply any place where an expression is being evaluated
to see whether it's true or false.
Void
4 This context not only doesn't care what the return value is, it doesn't even
want a return value.
Interpolative
5
This context only happens inside quotes, or things that work like quotes.