UNIT 3 Part 1
UNIT 3 Part 1
UNIT III
INTRODUCTION
TO PERL AND
SCRIPTING
Dr.D.Magdalene Delighta
Angeline
III CSE Associate Professor-CSE
Page 1 of 63
JBREC
What is Perl?
• 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.
• Perl is an Open Source software, licensed
under its Artistic License, or the GNU General
Public License (GPL).
• Perl was created by Larry Wall in 1987.
Page 2 of 63
Why Perl?
• System Administration
• Web and Perl: Perl can be embedded
into web servers to increase its
processing power and it has the DBI
package, which makes web-database
integration very easy.
Page 4 of 63
Perl Features
• Perl supports both procedural and object-
oriented programming.
• Perl works with HTML, XML, and other mark-
up languages.
• Perl is Interpreted.
Page 5 of 63
Perl Features
Page 6 of 63
Advantages of Perl
• Compared to other Programming languages Perl
is most powerful for text handling and Parsing.
• This is an interpreted language with fast
execution time as there is no need to compile a
Perl script
• Simple and easy to program and understand.
• It is object oriented.
• CPAN library eases Perl development.
• Used in Web development for mostly Payment
Gateways.
• Used in Automation and to test most of the
Network and Storage related stuff.
Page 7 of 63
Disadvantages of Perl
• There is minimal GUI support as compared to
other Programming languages.
• Need to refer to complex library modules
which are not so easy to understand.
• Understanding complex Patterns requires
experience.
Page 8 of 63
Scripts and programs
• Scripting is the action of typing scripts using a
scripting language, distinguishing neatly
between programs, which are written in
conventional programming language such as
C,C++,java, and scripts, which are written
using a different kind of language.
Page 9 of 63
Scripts and programs
• Building applications from ‘off the shelf’
(Software and hardware that already exists
and is available from commercial sources.)
components
• Controlling applications that have a
programmable interface.
• Writing programs where speed of
development is more important than run-
time efficiency.
Page 10 of 63
Scripts and programs
• The most important difference is that scripting
languages incorporate features that enhance
the productivity of the user in one way or
another, making them accessible to people
who would not normally describe themselves
as programmers, their primary employment
being in some other capacity.
Page 11 of 63
Origin of scripting
• The use of the word ‘script’ in a computing
context dates back to the early 1970s,when
the originators of the UNIX operating system
create the term ‘shell script’ for 6 sequence of
commands that were to be read from a file
and follow in sequence as if they had been
typed in at the keyword. e.g. an ‘AWKscript’, a
‘perl script’ etc. the name ‘script ‘ being used
for a text file that was intended to be
executed directly rather than being compiled
to a different form of file prior to execution.
Page 12 of 63
Origin of scripting
• In a DOS based system, use of a dial-up
connection to a remote system required a
communication package that used proprietary
language to write scripts to automate the
sequence of operations required to establish
a connection to a remote system.
Page 13 of 63
Origin of scripting
Page 16 of 63
What is glue language?
• They are especially useful for writing and
maintaining:
• Custom commands for a command shell
• Smaller programs than those that are better
implemented in a compiled language
• "Wrapper" programs for executables, like a batch
file that moves or manipulates files and does other
things with the operating system before or after
running an application like a word processor,
spreadsheet, data base, assembler, compiler, etc.
• Scripts that may change
• Rapid prototypes of a solution eventually
implemented in another, usually compiled,
language. Page 17 of 63
What is glue language?
• Using a scripting language to ‘manipulate,
customize and automate the facilities of an existing
system’, as the ECMA Script definition puts it.
• Here the script is used to control an application that
provides a programmable interface: this may be an
API, though more commonly the application is
constructed from a collection of objects whose
properties and methods are exposed to the scripting
language.
• Example: use of Visual Basic for applications to
control the applications in the Microsoft Office
Suite. Page 18 of 63
What is glue language?
• Using a scripting language with its rich
functionality and ease of use as an alternate to a
conventional language for general programming
tasks, particularly system programming and
administration.
• Examples are UNIX system administrators have
for a long time used scripting languages for
system maintenance tasks, and administrators of
WINDOWS NT systems are adopting a scripting
language ,PERL for their work.
Page 19 of 63
CS613PE SCRIPTING LANGUAGES
UNIT III
PERL- Names and
Values
Dr.D.Magdalene Delighta
Angeline
III CSE Associate Professor-CSE
Page 20 of 63
JBREC
Writing a Perl Script
• Perl scripts are just text files, so to “write” the
script, create a text file using text editor.
• Save the file with filename.pl.
• Execute the text file as follows
Page 21 of 63
Names
• Perl manipulates variables which have a name
(or identifier) and a value.
• A value is assigned to (or stored in) a variable by
an assignment statement of the form
name = value;
Page 22 of 63
Names
• Perl distinguishes between singular and plural
nouns (names).
• A singular name is associated with a variable
that holds a single item of data (a scalar value).
• A plural name is associated with a variable that
holds a collection of data items (array or hash).
Page 23 of 63
Names
• In Perl, the variable names start with a character that denotes
the kind of thing that the name stands for
– scalar data ($)
– array (@)
– hash (%)
– subroutine (&)
• The remainder of the name follows the more-or-Iess standard
convention: valid characters are letters, digits and
underscores, and the first character after the special character
must be a letter or underscore.
Page 24 of 63
Special Character
• Names may also have non-alphanumeric character after
special character.
$$,$?;
• The use of a character at the start of a name means that
each kind of data has a separate namespace.
• The character also determines the context in which the
name is used.
Page 25 of 63
Basic Naming Rules
• Variable names can start with a letter, a number, or
an underscore.
• Variables can start with a number, but they must be
entirely composed of that number; for example,
$123 is valid, but $1var is not.
• Variable names that start with anything other than a
letter, digit, or underscore are generally reserved for
special use by Perl.
• Variable names are case sensitive.
• Names all in uppercase are constants.
• All scalar values start with $, including those
accessed from an array of hash, for example
$array[0] or $hash{key}.
Page 26 of 63
Basic Naming Rules
• All array values start with @, including arrays or
hashes accessed in slices, for example
@array[3..5,7,9] or @hash{‘bob’, ‘alice’}
• All hashes start with %.
• Namespaces are separate for each variable
type—the variables $var, @var, and
%var are all different variables in their own right.
Page 27 of 63
Perl variables
• A variable comes into existence when declared
or first used with a special 'value' denoted by
undef.
• If this first occurrence of a variable is in a right
context, where a value is an empty string in a
undef evaluates to zero in a numeric context and
to context.
• it is possible to test this value, thus a program
can find out whether a variable has ever been
given a value.
Page 28 of 63
Perl variable Declaration
• Perl insist on declarations by placing the line
use strict ‘vars’;
or
use strict;
at the start of a script.
• Variables are declared explicitly using my
[Eg.] my x,y,z;
Page 29 of 63
Example
#!/usr/bin/perl OUTPUT:
@ages = (25, 30, 40); $ages[0] = 25
@names = ("John Paul", "Lisa", $ages[l] = 25
"Kumar"); $ages[2] = 40
print "\$ages[0] = $ages[0]\n"; $names[0] = John Paul
$names[1] = Lisa
print "\$ages[1] = $ages[1]\n"; $names[2] = Kumar
print "\$ages[2] = $ages[2]\n";
print "\$names[0] = $names[0]\n";
print "\$names[1] = $names[1]\n";
print "\$names[2] = $names[2]\n“;
Page 30 of 63
Perl variables
Perl Variables/Data
Types
Page 31 of 63
Perl variable
• Variables are the reserved memory locations to
store values.
• When variables are created they reserve some
memory space.
• The equal sign (=) is used to assign values to
variables.
• At the left of (=) is the variable name and on the
right it is the value of the variable.
Example:
$name = "Anastasia";
$rank = "9th";
$marks = 756.5;
Page 32 of 63
Scalar Data
• Scalars contain a single string or numeric value.
The variable name must start with a $.
• The data might be an integer number, floating
point, a character, a string, a paragraph, or an
entire web page.
Page 33 of 63
Scalar Data - String
• In Perl, a string is a sequence of characters
surrounded by some kinds of quotation marks.
Example:
my $s1 = “Hello World!";
my $s2 = ' Hello World!';
Page 34 of 63
Scalar Data - Numbers
• Integers are whole numbers that have no digits
after the decimal points i.e 10, -20 or 100.
• In Perl, integers are often expressed as decimal
integers, base 10.
Example:
$x = 20;
$y = 100;
$z = -200;
Page 35 of 63
Scalar Data – Boolean Values
• A boolean context is a place where expression is
evaluated just to check whether it is true or
false.
Page 36 of 63
Scalar data - Example
$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";
Output:
Age = 25
Name = John Paul
Salary = 1445.5
Page 37 of 63
Array
• 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.
Page 38 of 63
Array -Example
@age = (25, 30, 40);
@name = ("John Paul", "Lisa", "Kumar");
print "\$age [0] = $age [0]\n";
print "\$age [1] = $age [1]\n";
print "\$name [0] = $name [0]\n";
print "\$name [1] = $name [1]\n";
Output:
$age [0] = 25
$age [1] = 30
$name [0] = John Paul
$names[1] = Lisa Page 39 of 63
Hash
• A hash is a set of key/value pairs.
• Variables of the Hash type are preceded by a
modulus (%) sign.
• Keys are used to refer to a single variable in the
Hash.
• To access these elements, Hash variable name
followed by the Key associated with the value is
used in curly brackets.
Page 40 of 63
Hash
• A hash is a set of key/value pairs.
• Variables of the Hash type are preceded by a
modulus (%) sign.
• Keys are used to refer to a single variable in the
Hash.
• To access these elements, Hash variable name
followed by the Key associated with the value is
used in curly brackets.
Page 41 of 63
Hash -Example
%data = (‘Lisa', 55, ‘Kumar', 80);
print "\$data{‘Lisa'} = $data{‘Lisa'}\n";
print "\$data{‘Kumar'} = $data{‘Kumar'}\n";
Output:
$data{‘Lisa'} = 55
$data{‘Kumar'} = 80
Page 42 of 63
Constant / Literal
• A hash is a set of key/value pairs.
• Variables of the Hash type are preceded by a
modulus (%) sign.
• Keys are used to refer to a single variable in the
Hash.
• To access these elements, Hash variable name
followed by the Key associated with the value is
used in curly brackets.
Page 43 of 63
Numeric Constant
• Perl stores all the numbers internally as either
signed integers or double-precision floating-
point values.
Example
1234
-100
2500
16.12E
0xfff
Page 44 of 63
String Constant
• Strings are sequences of characters.
• They are usually alphanumeric values delimited
by either single (') or double (") quotes.
Example
$str = "Welcome!";
print "$str\n";
$str = ‘Hello!';
Print "$str\n";
Output:
Welcome!
Hello! Page 45 of 63
CS613PE SCRIPTING LANGUAGES
UNIT III
Arrays, list,
hashes
Dr.D.Magdalene Delighta
Angeline
III CSE Associate Professor-CSE
Page 46 of 63
JBREC
List
• A list is a collection of variables, constants
(numbers or strings) or expressions.
• It is written as a comma-separated sequence of
values.
• A list often appears in a script enclosed in round
brackets.
• The elements of a list can be accessed using
indexes. Index starts with 0.
Page 47 of 63
List - Example
@list1 = (1, 2, “Hello", “Good", “Morning");
print "Complex List: @list1";
Output:
1 2 Hello Good Morning
Page 48 of 63
Defining Range in a List
• The range can be specified in a list.
Syntax:
leftValue..rightValue
Example:
@x = ("a".."j");
print "List with elements from a to j: @x\n";
Output:
List with elements from a to j: a b c d e f g h i j
Page 49 of 63
List Magic
• Lists are often used in connection with arrays
and hashes.
Example:
($a, $b, $c) =(1, 2, 3);
Page 50 of 63
Arrays
• An array is an ordered collection of data whose
components are identified an ordinal index: it is
usually the value of an array variable.
• The name of such a variable always starts with
an @.
• Example:
@rainfall=(1.2, 0.4, 0.3, 0.1, 0, 0, 0);
Page 51 of 63
Arrays - List within a list
• A list can occur as an element of another list.
Example:
@foo= (1,2, 3, "string");
@foobar = (4, 5, @foo, 6);
print “@foobar”
Output
4, 5, 1,2, 3, 6
Page 52 of 63
Arrays - List and scalar context
• List context implies that the 'target' of an
operation is a collection, while scalar context
implies that it is a single data item.
Example:
@foo =(101, 102,103);
$n =@foo;
print "array foo has $n elements\n"
Output
array foo has 3 elements
Page 53 of 63
Working with arrays and lists
• Perl a number of powerful facilities for accessing
collections of array elements.
• A group of contiguous elements is called a slice.
Syntax:
@foo[1..3]
Example:
@foo =( “hop”, “skip”, “jump”);
Page 54 of 63
Manipulating lists
• Perl provides several built-in functions for list
manipulation.
Built-in Function for List
Manipulation
Page 55 of 63
Manipulating lists
• shift LIST. Returns the first item of LIST, and
moves the remaining items down,reducing the
size of LIST by 1.
• unshift ARRAY, LIST. puts the items in LIST at the
beginning of ARRAY, moving the original
contents up by the required amount.
• push ARRAY, LIST. adds the values in LIST to the
end of ARRAY.
• pop ARRAY, LIST. removes the values in LIST from
the end of ARRAY.
Page 56 of 63
Manipulating lists - Example
@x=("java","Ruby","c++","c");
push(@x,python);
print("After push:@x\n");
pop(@x);
print("After pop:@x\n");
unshift(@x,php,sql);
print("After unshift:@x\n");
shift(@x);
print("After Shift:@x");
Output:
• After push:java Ruby c++ c python
• After pop:java Ruby c++ c
• After unshift:php sql java Ruby c++ c
• After Shift:sql java Ruby c++ c
Page 57 of 63
Hashes
• A hash is a set of key-value pairs.
• Perl stores elements of a hash such that it
searches for the values based on its keys.
• Hash variables start with a ‘$’ sign.
Example:
$a{234}=“Hai”;
Page 58 of 63
Creating hashes
• A list can be assigned to an array
Syntax:
%foo =(key1, value1, key2, value2,…);
Example:
%foo= (banana => 'yellow',
apple => ' green‘ ) ;
Page 59 of 63
Manipulating hashes
• A hash can be 'unwound' into a list containing
the Key-value pairs by assigning it to an array.
Example:
@list =%foo;
Page 60 of 63
Manipulating hashes
Magic:
• keys %magic
– returns a list of the of the elements in the hash.
• values %magic
– returns a list of the values of the elements in the hash.
Delete:
• delete $magic{$key}
– removes the element whose key matches $key from the
hash %magic
Exists:
• exists $magic{$key}
– returns true if the hash %magic contains an element
whose key matches $key.
Page 61 of 63
Manipulating hashes - Example
foreach $key (keys %magic) {
do something with $magic{$key}
}
Page 62 of 63
THANK YOU!
Page 63 of 63