0% found this document useful (0 votes)
6 views

Perl

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Perl

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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

$perl -e 'print "Hello World\n"'


Script 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.

• A Perl variable name starts with either $, @ or % followed by zero or more


letters, underscores, and digits (0 to 9).

• Perl does not allow punctuation characters such as @, $, and % within identifiers.

• Perl is a case sensitive programming language.

Thus $hello and $Hello are two different identifiers in Perl.


Perl - Data Types
• Perl has three basic data types: scalars, arrays of scalars, and hashes of scalars,
also known as associative arrays.

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

• Strings are sequences of characters.


• They are usually alphanumeric values delimited by either single (') or double (")
quotes.
• Double-quoted string literals allow variable interpolation, and single-quoted
strings are not.
• There are certain characters when they are proceeded by a back slash, have
special meaning and they are used to represent like newline (\n) or tab (\t).

Escape sequence Meaning


\\ Backslash
\' Single quote
\" Double quote
Escape
Meaning
sequence
\a Alert or bell
\b Backspace
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\0nn Creates Octal formatted numbers
\xnn Creates Hexideciamal formatted numbers
Escape
Meaning
sequence

\cX Controls characters, x may be any character

\u Forces next character to uppercase

\l Forces next character to lowercase

\U Forces all following characters to uppercase

\L Forces all following characters to lowercase

\Q Backslash all following non-alphanumeric characters


\E End \U, \L, or \Q
OUTPUT:
Perl - Variables
• Variables are the reserved memory locations to store values.
• This means that when you create a variable you reserve some space in
memory.
• Based on the data type of a variable, the interpreter allocates memory and
decides what can be stored in the reserved memory.
• Therefore, by assigning different data types to variables, you can store
integers, decimals, or strings in these variables.
• Perl has 3 data types : Scalars, Arrays, Hashes.
• A scalar variable will precede by a dollar sign ($) and it can store either a
number, a string, or a reference.
• An array variable will precede by sign @ and it will store ordered lists of
scalars.
• The Hash variable will precede by sign % and will be used to store sets of
key/value pairs.
• We can use the same name for a scalar variable, an array, or a hash. This
means that $hello and @hello are two different variables.
Creating Variables
• Perl variables do not have to be explicitly declared to reserve memory space.
• The declaration happens automatically when you assign a value to a variable.
• The equal sign (=) is used to assign values to variables.
• The operand to the left of the = operator is the name of the variable, and the
operand to the right of the = operator is the value stored in the variable.

$age = 25; # An integer assignment


$name = "John Paul"; # A string
$salary = 1445.50; # A floating point
Scalar Variables
• A scalar is a single unit of data.
• That data might be an integer number, floating point, a character, a string, a
paragraph, or an entire web page

#!/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);

print "\$data{'John Paul'} = $data{'John Paul'}\n";


print "\$data{'Lisa'} = $data{'Lisa'}\n";
print "\$data{'Kumar'} = $data{'Kumar'}\n";
Variable Context
Perl treats same variable differently based on Context, i.e., situation where a variable is
being used

#!/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.

You might also like