0% found this document useful (0 votes)
20 views140 pages

Unit 3

The document discusses scripting languages, their origins, characteristics, and modern applications. It highlights the differences between scripting and conventional programming, emphasizing the ease of use and rapid development capabilities of scripting languages. The document also covers various uses for scripting, including system administration, web scripting, and visual scripting, showcasing their importance in contemporary software development.

Uploaded by

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

Unit 3

The document discusses scripting languages, their origins, characteristics, and modern applications. It highlights the differences between scripting and conventional programming, emphasizing the ease of use and rapid development capabilities of scripting languages. The document also covers various uses for scripting, including system administration, web scripting, and visual scripting, showcasing their importance in contemporary software development.

Uploaded by

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

Scripting Languages

Unit III
Department of Information Technology

BVRIT HYDERABAD College of Engineering for Women


Contents
Scripts and Programs, PERL
• Origin of Scripting, • Names and Values,
• Scripting Today, • Variables,
• Characteristics of Scripting Languages, • Scalar Expressions,
• Control Structures,
• Uses for Scripting Languages,
• arrays, list, hashes, strings,
• Web Scripting, • pattern and regular
• the universe of Scripting Languages. expressions,
• subroutines

BVRIT HYDERABAD College of Engineering for Women


Introduction
• Scripting is the action of writing 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.
• The use of scripting languages is just another kind of programming.
• Scripting languages are used for is qualitatively different from
conventional programming languages like C++ and Ada address the
problem of developing large applications from the ground up, employing
a team of professional programmers, starting from well-defined
specifications, and meeting specified performance constraints.

BVRIT HYDERABAD College of Engineering for Women


• Scripting languages, on other hand, address different problems:

• Building applications from ‘off the shelf’ components


• Controlling applications that have a programmable interface
• Writing programs where speed of development is more important than
run-time efficiency.
• 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.

BVRIT HYDERABAD College of Engineering for Women


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

BVRIT HYDERABAD College of Engineering for Women


• Other early occurrences of the term ‘script’ can be found. For example, 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.

• Note that if we regard a scripts as a sequence of commands to control an


application or a device, a configuration file such as a UNIX ‘make file’
could be regard as a script. However, scripts only become interesting
when they have the added value that comes from using programming
concepts such as loops and branches.

BVRIT HYDERABAD College of Engineering for Women


Scripting today
SCRIPTING IS USED WITH 3 DIFFERENT MEANINGS

1. A new style of programming which allows applications to be developed much


faster than traditional methods allow, and makes it possible for applications to
evolve rapidly to meet changing user requirements.

• This style of programming frequently uses a scripting language to interconnect ‘off


the shelf ‘ components that are themselves written in conventional language.

• Applications built in this way are called ‘glue applications’ ,and the language is
called a ‘glue language’.

BVRIT HYDERABAD College


BVRIT HYDERABAD College ofof Engineering
Engineering for Women
for Women
• A glue language is a programming language i,e an interpreted
scripting language and is designed or suited for writing glue code –
code is to connect software parts. They are especially useful for
writing and maintaining:
o Custom commands for a command shell
o Smaller programs than those that are better implemented in a compiled
language
o "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.
o Scripts that may change
o Rapid prototypes of a solution eventually implemented in another, usually
compiled, language.

BVRIT HYDERABAD College of Engineering for Women


• Glue language examples:

BVRIT HYDERABAD College of Engineering for Women


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

BVRIT HYDERABAD College of Engineering for Women


3. 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: 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.

BVRIT HYDERABAD College of Engineering for Women


Characteristics of scripting languages
These are some properties of scripting languages which differentiate SL from
programming languages.

• Integrated compile and run:


• SL’s are usually characterized as interpreted languages, but this is just
an oversimplification.
• They operate on an immediate execution, without a need to issue a
separate command to compile the program and then to run the
resulting object file, and without the need to link extensive libraries
into the object code. This is done automatically.
• A few SL’S are indeed implemented as strict interpreters.

BVRIT HYDERABAD College of Engineering for Women


• Low overheads and ease of use:
• Variables can be declared by using the number of different data
types is usually limited everything is a string by context it will be
converted as number(vice versa).
• Number of data structures is limited(arrays)

• Enhanced functionality:
• SL’s usually have enhanced functionality in some areas.
• For example, most languages provide string manipulation based on
the use of regular expressions, while other languages provide easy
access to low-level operating system facilities, or to the API, or
object exported by an application.

BVRIT HYDERABAD College of Engineering for Women


• Efficiency is not an issue: ease of use is achieved at the expense of
efficiency because efficiency is not an issue in the applications for
which SL’S are designed.

• A scripting language is usually interpreted from source code or


bytecode. By contrast, the software environment the scripts are
written for is typically written in a compiled language and distributed
in machine code form.

• Scripting languages may be designed for use by end users of a


program – end-user development – or maybe only for internal use by
developers, so they can write portions of the program in the scripting
language.

BVRIT HYDERABAD College of Engineering for Women


• Scripting languages typically use abstraction, a form of information
hiding, to spare users the details of internal variable types, data
storage, and memory management.
• Scripts are often created or modified by the person executing
them, but they are also often distributed, such as when large
portions of games are written in a scripting language.

• The characteristics of ease of use, particularly the lack of an explicit


compile-link-load sequence, are sometimes taken as the sole
definition of a scripting language.

BVRIT HYDERABAD College of Engineering for Women


Uses for Scripting Languages
1. Traditional scripting
The activities that comprise traditional scripting include
• System administration automating everyday tasks, building data
reduction tools
• Controlling application remotely
• System and application extensions
• 'Experimental’ programming
• Building command-line interfaces to applications based on C libraries
• Server-side form processing on the Web using CGI.

BVRIT HYDERABAD College of Engineering for Women


System administration
• The concept of scripting first arose out of the requirements of system
administration in the UNIX world.
• The administrators used shell scripts-sequences of shell commands
executed from a file to automate everyday tasks such as adding a new
user to the system or backing up the file system.
• With the advent of the programmable Bourne shell and its successors,
sophisticated effects could be obtained with shell scripts, which
became viable alternatives to C Programming for most activities of this
kind.

BVRIT HYDERABAD College of Engineering for Women


• Indeed, the Bourne shell can claim to be one of the first scripting
languages: the other was REXX, developed at about the same time,
which provided a similar kind of programmable control over the IBM
mainframe VM/CMS system.
• The Bourne shell was well suited to system administration since (i) it
was fully programmable and (ii) it provided full access to the
underlying system calls.
• This is vital since system administration is more about manipulating
files and processes than performing numerical computations.
• The other requirement for system administration is the ability to
process the contents of the files, e.g. for digesting log files and
presenting the results in a readable format, tabulating disk usage etc.
as well as manipulating the files themselves.
BVRIT HYDERABAD College of Engineering for Women
Driving applications remotely
• An early application of scripting was to control a dial-up link to a
remote computer.
• The use of a scripting language, rather than a utility that just plays
back a previously recorded sequence of keystrokes, is necessary
because there is often an element of conditional logic involved.
• For example, having dialled the number it is necessary to wait until
the distant computer responds: it may then be necessary to
acknowledge the response and wait for the 'login' prompt, and,
having sent the user identifier, wait again for the 'password:' prompt.
Any of these stages may fail, and so the script must also include
conditional logic to deal with failures, and retry the operation if
appropriate.

BVRIT HYDERABAD College of Engineering for Women


System and application extensions

• The UNIX system is a scriptable application, since it exposes a programmable API


in the form of the system calls.
• A shell script could be regarded as an example of automation, in which the
command interpreter is driven by a script held in a file rather than by a real user.
• This kind of behaviour is taken as application extension rather than as
application automation.
• The distinction can perhaps be made clear by considering the changing nature of
macros in desktop applications such as word processors and spreadsheet
programs.
• From the very early days these applications provided automation of common
sequences of operations in the form of keystroke recording and subsequent
replay.
• A modern form of extension is found in the use of scripting to provide an
'escape' from a declarative system for tasks that an element of programmability.

BVRIT HYDERABAD College of Engineering for Women


• Experimental programming
• Traditional languages like C were developed as tools for writing large and
complex programs, many of which require teams of programmers.
• Not all programming is like that: as the early system administrators is
covered there is another class of programs, the writing of which involves
experimentation, since the precise requirements are not clear at the outset,
and it is often necessary to produce several versions before the tool is
satisfactory.
• Low-overhead scripting languages are well suited to this kind of
'experimental programming' since ideas can be tried out without incurring a
time consuming penalty from the overhead of the edit-compile-link-load
cycle.

BVRIT HYDERABAD College of Engineering for Women


• The convenience of Perl for this kind of programming caused its use to
spread beyond the confines of system programming as a tool for rapid
application development and for building prototypes-an inherent part
of the software development process, which leads to a better-quality
product in the end.
• Sometimes a prototype in Perl is a prelude to a final application coded
in C or C++: however, unless there are strong requirements for run-time
efficiency, the application may never be re-coded - the last prototype'
becomes the production version.

BVRIT HYDERABAD College of Engineering for Women


Command-line interfaces
• A major use of scripting languages in traditional scripting is as a form of
'glue' to connect together sections of code written in some other
language through a command-line interface, making it possible to build
new applications by combining other applications, or part., of other
applications.
• In the UNIX world, a glue language is one that is able to start up
another program, collect its output, process it and perhaps pass it as
input to a third program, and so on.
• The shell offers a simple form of glue in the form of pipes, and
languages like Perl provide much more comprehensive glue capabilities.

BVRIT HYDERABAD College of Engineering for Women


• An extension to this line of thought is the idea that large software
systems should be developed using a combination of two one, such as
C, C++ for manipulating the complex internal data structures and
other operations for which performance is important, and a scripting
language for writing scripts that glue the C pieces together.
• In this way we can speed the development of powerful and
media-rich applications that can call upon compiled routines written
in an efficient low-level language when want to.
• Tcl is a very good of this approach to development.

BVRIT HYDERABAD College of Engineering for Women


Modern scripting
• To complement our discussion of traditional scripting, we now
survey the world of modern scripting based on scriptable objects.
• The applications that comprise modern Scripting include:
• Visual scripting
• Using scriptable components - macros for desktop applications
and compound documents
• Client-side and server-side Web Scripting.

BVRIT HYDERABAD College of Engineering for Women


Visual scripting
• Visual scripting is the process of constructing a graphical interface from
a collection of visual objects which have properties (attributes), e.g.
foreground and background colours, text on a button etc. that can be set
by a program written in an appropriate language.
• Some objects (i.e, buttons) respond to external events such as mouse
clicks and the response to each action is defined by a script.
• Programming such an interface is described as ‘scripting the objects’.
• Visual Basic is the pre-eminent visual scripting system, used to develop
new applications and to provide visual interfaces for legacy applications,
either as prototypes to be replaced later by production versions written
in C++, or as final products in their own right, the objects being scripted
using a dialect of the BASIC language, confusingly also called Visual
Basic.
BVRIT HYDERABAD College of Engineering for Women
• Alternatives to Visual Basic for visual scripting in the Windows
environment include Tcl/Tk and Perl-Tk.
• In the UNIX environment Tcl/Tk, Perl-Tk and the Desktop Kornshell
facilitate the construction of visual interfaces under the X Window
system: these are much more accessible than the alternative of using
one of the C-based X Window system development libraries.
• In all these cases we see the power of scripting: controls (widget),
created by professional programmers using C++, can easily be
assembled (‘glued') into applications using a scripting language,
without necessarily requiring expert programming skills.
• Another use of visual is creating enhanced Web pages.

• Tcl/Tk -Tool Command Language/Tool Kit,

BVRIT HYDERABAD College of Engineering for Women


Scriptable components
• In this modern world the idea of glue reappears in the use of scripting
languages to manipulate collection of objects or components which
expose an interface that conforms to a particular scripting architecture:
in brief, scriptable objects.
• The first applications to be built round the concept of scriptable objects
were Microsoft's Visual Basic and Excel: the concept has since developed
to underpin all of Microsoft's application development.

BVRIT HYDERABAD College of Engineering for Women


Scriptable objects include:
• Visual components (‘controls') whose properties can be manipulated by
a scripting language. These may form part of an application like Visual
Basic, or they may be embedded in an application such as a Web
browser to provide dynamic interaction.
• Parts of a compound document, e.g. a spreadsheet embedded in a
word-processor document.
• More substantial 'component objects' encapsulating the data and
functionality of all or part of an application, e.g. the spellchecker in a
word processor, an interface to an external database or a low-level
interface to the Internet.
• Elements in an object model that reflects the structure of some entity
of interest to a user, e.g. a Web page.

BVRIT HYDERABAD College of Engineering for Women


Web scripting
• The Web is one of the most fertile areas for the application of
scripting languages at the present time, and readers of the popular
technical press probably believe that scripting is solely concerned
with generating interactive Web pages.
• Web scripting divides into three areas:
(i) processing forms,
(ii) creating pages with enhanced visual effects and user interaction
and
(iii) generating pages 'on the fly’ from material held in a database.

BVRIT HYDERABAD College of Engineering for Women


Processing Web forms
• From the early days, HTML has allowed a limited kind of user
interaction through the use of forms - a rather primitive visual
interface defined by tags in an HTML document.
• In the original implementation of the Web, when the form is
submitted for processing the information entered by the user is
encoded and sent to the server for by processing a CGI script that
eventually generates an HTML page to be sent back to the Web
browser.
• This processing requires string manipulation to decode the form data
and text manipulation to construct the HTML page that constitutes
the reply, and may also require system access, to run other processes
and/or establish network connections.

BVRIT HYDERABAD College of Engineering for Women


• These are exactly the requirements that we have already identified as
those of the UNIX system administrator, and it is not surprising that the
system administrator's favourite language, Perl, is also the language of
choice for CGI scripting.
• As an alternative to doing all the processing of a form with a script
running on the server, it is possible to do some client-side processing
within the browser to validate form data before sending it to the
server, using a scripting language designed for the purpose, e.g.
JavaScript/JScript or VBScript.

BVRIT HYDERABAD College of Engineering for Women


Dynamic Web pages
• Dynamic HTML Implemented in Internet Explorer makes every component
of a Web page headings, anchors, emphasized text, tables, form
components etc. - a scriptable object.
• This makes it possible to provide very elaborate dynamic control over the
appearance of a Web page and to provide simple interaction with the user
using scripts written in JavaScript/JScript or VBScript, which are
interpreted by the browser.
• Scripts can also be used to obtain information about the browsers and
thus optimize pages to match the browser's capabilities.
• Microsoft's ActiveX technology (so far available only in Internet Explorer)
allows the creation of pages with more elaborate user interaction by using
embedded visual objects called ActiveX controls (analogous to the
controls in Visual Basic).

BVRIT HYDERABAD College of Engineering for Women


• These controls are scriptable objects, and can in fact be scripted in a
variety of languages, since Explorer implements a scripting
architecture called ActiveX Scripting.
• An ActiveX scripting host does not interface directly to an embedded
scripting language; instead it can access any of a number of scripting
engines to run a script written in the language associated with the
particular engine.
• Thus, while Internet Explorer comes with scripting engines for VBScript
and JavaScript (JScript), it can also be scripted in Perl using a Perl
scripting engine supplied by a third party.
• The ActiveX and OLE(Object Linking and Embedding) specifications are
non-proprietary, and so in principle ActiveX scripting need not be
limited to Microsoft language and applications.

BVRIT HYDERABAD College of Engineering for Women


The universe of scripting languages
• In describing the world of scripting languages we have identified
traditional and modern scripting, and we have seen that Web scripting
forms an important part of modern scripting. An alternative view is that
there is a scripting universe, containing multiple overlapping worlds:
• the original UNIX world of traditional scripting using Perl and Tel,
which are now available on most platforms
• the Microsoft world of Visual Basic and ActiveX controls
• the world of VBA for scripting compound documents
• the world of client-side and server-side Web scripting.

BVRIT HYDERABAD College of Engineering for Women


PERL
• Perl is a programming language developed by Larry Wall,
especially designed for text processing.

• 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
Names
• Like any other programming language, Perl manipulates variables
which have a name (or identifier) and a value: a value is assigned to a
variable by an assignment statement of the form
name=value;
• Variable names resemble nouns in English, and like English, Perl
distinguishes between singular and plural nouns.
• 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 (an array or hash).

BVRIT HYDERABAD College of Engineering for Women


• A notable characteristic of Perl is that variable names start with a
special character that denotes the kind of thing that the name stands
for - scalar data ($), array (@), hash (%), subroutine (&) etc.
• The syntax also allows a name that consists of a single
non-alphanumeric character after the initial special character, eg. $$,
$?; such names are usually reserved for the Perl system.
• If we write an assignment, eg. j=j+1, the occurrence of j on the left
denotes a storage location, while the right-hand occurrence denotes
the contents of the storage location.
• We sometimes refer to these as the lvalue and rvalue of the variable:
more precisely we are determining the meaning of the identifier in a
left-context or a right-context.

BVRIT HYDERABAD College of Engineering for Women


• In the assignment a[j] = a[j] + 1, both occurrences of j are determined
in a right-context, even though one of them appears on the left of the
assignment.
• In conventional programming languages, new variables are
introduced by a declaration, which specifies the name of the new
variable and also its type, which determines the kind of value that can
be stored in the variable and, by implication, the operations that can
be carried out on that variable.

BVRIT HYDERABAD College of Engineering for Women


Strings and numbers
• In common with many scripting languages, Perl recognizes just two
kinds of scalar data: strings and numbers.
• There is no distinction between integer and real numbers as
different types.
• Perl is a dynamically typed language: the system keeps track of
whether a variable contains a numeric value or a string value, and
the user doesn't have to worry about the difference between strings
and numbers since conversions between the two kinds of data are
done automatically as required by the context in which they are
used.

BVRIT HYDERABAD College of Engineering for Women


Boolean values
• All programming languages need some way of representing truth
values and Perl is no exception.
• Since scalar values are either numbers or strings, some convention is
needed for representing Boolean values, and Perl adopts the simple
rule that numeric zero, "0" and the empty string (" ") mean false, and
anything else means true.

BVRIT HYDERABAD College of Engineering for Women


Numeric constants
• Numeric constants can be written in a variety of ways, including
specific notation, octal and hexadecimal.
• Although Perl tries to emulate natural human communication, the
common practice of using commas or spaces to break up a large
integer constant into meaningful digit groups cannot be used, since
the comma has a syntactic significance in Perl.
• Instead, underscores can be included in a number literal to improve
legibility.

BVRIT HYDERABAD College of Engineering for Women


Variables and assignment
Assignment
• Borrowing from C, Perl uses '=' as the assignment operator. It is
important to note that an assignment statement returns a value, the
value assigned.
• This permits statements like
$b = 4 + ( $a = 3) ;
• which assigns the value 3 to $a and the value 7 to $b.
• If it is required to interpolate a variable value without an intervening
space the following syntax, borrowed from UNIX shell scripts , is used:
$a = "Java ;
$b = "$ { a } Script" ; which gives $b the value "JavaScript"

BVRIT HYDERABAD College of Engineering for Women


<STDIN> - a special value
• When the 'variable' <STDIN> appears in a context where a scalar
value is required, it evaluates to a string containing the next line from
standard input, including the terminating newline.
• If there is no input queued, Perl will wait until a line is typed and the
return key pressed.
• The empty string is treated as false in a Boolean context. If <STDIN>
appears on the right-hand side of an assignment to a scalar variable,
the string containing the input line is assigned to the variable named
on the left.
• If it appears in any other scalar context the string is assigned to the
anonymous variable: this can be accessed by the name $- : many
operations use it as a default.

BVRIT HYDERABAD College of Engineering for Women


Scalar Expressions
• Scalar data items are combined into expressions using operators.
• Perl has a lot of operators, which are ranked in 22 precedence levels.
• These are carefully chosen so that the ‘obvious’ meaning is what You
get , but the old advice still applies: if in doubt ,use brackets to force
the order of evaluation .
• In the following sections we describe the available operators in their
natural groupings-arithmetic , strings, logical etc .

BVRIT HYDERABAD College of Engineering for Women


Arithmetic operators
• Following the principles of ‘no surprises’ Perl provides the usual Arithmetic
operators, including auto-increment and auto-decrement operators after
the manner of C: note that in
$c= 17 ; $d= ++$c;
• The sequence is increment and the assign, whereas in
$c= 17 ; $d = $c++;
• The sequence is assign then increment . As C, binary arithmetic operations
can be combined with assignment, e.g.
$a += 3;
• This adds 3 to $a, being equivalent to
$a =$a + 3;
• As in most other languages, unary minus is used to negate a numeric value;
an almost never- used unary plus operator is provided for completeness.

BVRIT HYDERABAD College of Engineering for Women


String Operators
• Perl provides very basic operators on strings: most string processing is one
using built-in functions expressions, as described later.
• Unlike many languages use + as a concatenation operator for strings, Perl
uses a period for this purpose: this lack of overloading means that an
operator uniquely determines the context for its operands.
• The other string operator is x, which is used to replicate strings, e.g.
$a =”Hello” x 3;
• Sets $a to “HelloHelloHello”.
• The capability of combining an operator with assignment is extended to
string operations. E.g.
$foo .= “ “ ;
• Appends a space to $foo.

BVRIT HYDERABAD College of Engineering for Women


• So far, things have been boringly conventional for the most part.
• However, we begin to get a taste of the real flavor of perl when we see how
it adds a little magic when some operators, normally used in arithmetic
context, are used in a string context.
• Two examples illustrate this.

1.Auto increment
• If a variable has only ever been used in a string context, the auto increment
operator can be applied to it.
• If the value consists of a sequence of letters, or a sequence of letters
followed by a sequence of digits, the auto increment takes place in string
mode starting with the right most character, with ‘carry’ along the string.
For example, the sequence
$a = ‘a0’ ; $b = ‘Az9’ ;
Print ++$a, ‘ ‘, ++$b; “/n”; Prints a1 Ba0.
BVRIT HYDERABAD College of Engineering for Women
Operators
Arithmetic Operator
• a=10, b=20
• Example − $a + $b will give 30

• Example − $a - $b will give -10

• Example − $a * $b will give 200

• Example - $b % $a will give 0

• Example - $a**$b will give 10 to the power 20


Equality Operators
• ($a == $b) is not true.
Lt
• ($a != $b) is true Gt
Le
Ge
• ($a > $b) is not true. Eq
Ne
cmp
• ($a < $b) is true.

• >= (greater than or equal to)

• <= (less than or equal to)


Quote
Like
Operat
ors
Others
Comparison operators
• The value of comparisons is returned as 1 if true, and an empty string (“
”) if false, in accordance with the convention described earlier.
• Two families of comparison operators provide, one for numbers and one
for strings.
• The operator used determines the context , and perl converts the
operands as required to match the operator.
• This duality is necessary because a comparison between strings made up
entirely numerical digits should apply the usual rules for sorting strings
ASCII as a collating sequence, and this may not give the same result as
the numerical comparison(‘5’ <’10’) returns the value true as a numerical
comparison having been converted into (5<10) where as the string
comparison (‘5’ lt ‘10’) returns false, since 10 comes before 5 in the
canonical sort order for ASCII strings.
BVRIT HYDERABAD College of Engineering for Women
• The comparison operator ( [] for numbers, cmp for strings), performs
a three way test,
• returning -1 for less-than,
0 for equal and
+1 for greater-than.
• Note that the comparison operators are non associative, so an
expression like $a > $b > $c Is erroneous.

BVRIT HYDERABAD College of Engineering for Women


Logical operators
• The logical operators allows to combine conditions using the usual
logical operations ‘not’(!, not), ‘and’(&&,and) and ‘or’(||,or).
• Perl implements the ‘and’ and ‘or’ operators in ‘shortcut’ mode, i.e
evaluation stops as soon as the final result is certain using the rules
false &&b=false, and true||b=true.
• Before Perl 5,only the !, && and || operators were provided.
• The new set, not, and ,or, are provided partly to increase readability,
and partly because their extra-low precedence makes it possible to
omit brackets in most circumstances-the precedence ordering is
chosen so that numerical expressions can be compared without
having to enclose them in brackets, e.g. Print” OK\n” if $a<10 and
$b<12;
BVRIT HYDERABAD College of Engineering for Women
Conditional expressions
• A conditional expression is one whose values is chosen from two
alternatives at run-time depending on the outcome of a test.
• The syntax is borrowed from C
Test ? true_exp: false_exp
• The first expression is evaluated as Boolean value : if it returns true
the whole expression is replaced by true_exp, otherwise it is replaced
by false_exp, e.g.
$a= ($a<0)? 0 : $a;

BVRIT HYDERABAD College of Engineering for Women


Control structures
• The Control Structures for conditional execution and repetition all the
control mechanisms is similar to C.

BLOCKS
• A block is a sequence of one or more statements enclosed in curly
braces. Eg: { $positive =1;$negative=-1;}
• The last statement is the block terminated by the closing brace.
• In, Perl they use conditions to control the evaluation of one or more
blocks.
• Blocks can appear almost anywhere that a statement can appear such
a block called bare block.
BVRIT HYDERABAD College of Engineering for Women
• Conditions
• A condition is a Perl expression which is evaluated in a Boolean
context: if it evaluates to zero or the empty string the condition is
false, otherwise it is true.
• Conditions usually make use of relational operators. Eg: $total>50
$total>50 and $total<100
• Simple Conditions can be combined into a complex condition using
the logical operators.
• A condition can be negated using the ! operator.
Eg: !($total>50 and $total<100)

BVRIT HYDERABAD College of Engineering for Women


CONDITIONAL EXECUTION
• If-then-else statements
if ($total>0)
{
print “$total\n”- if ($total>0)
{
print “$total\n”
}
else
{
print “bad total!\n”-
}
}

BVRIT HYDERABAD College of Engineering for Women


• A single statement is a block
and requires braces round Eg: if ($total>70)
it. {
$grade=”A”;
• The if statement requires }
that the expression forming elsif ($total >50)
the condition is enclosed in {
brackets. $grade=”B”;
• The construct extends to }
multiple selections elsif ($total>40)
{
$grade=”C”;
} else {
$grade=”F”;
$total=0;
}
BVRIT HYDERABAD College of Engineering for Women
Alternatives to if-then-else
• To use a conditional expression in place of an if-then-else construct.
if ($a<0)
($b=0)
else
($b=1)
• can be written as
$b= ($a<0)? 0:1;
• To use the ‘or’ operator between statements Eg: open (IN, $ARGV[0]
or die
• “Can’t open $ARGV*0+\n”;

BVRIT HYDERABAD College of Engineering for Women


Statement qualifiers
• A single statement(not a block) can be followed by a conditional modifier.
Eg: print “OK\n”
if $volts>=1.5;
print “Weak\n”
if
$volts>=1.2 and
$volts<1.5;
print “Replace\n”
if $volts<1.2;
• Code using Conditional expressions,
Eg: print (($volts>=1.5)? “Ok\n”; (($volts>=1.2)? “Weak\n”; “Replace\n”));

BVRIT HYDERABAD College of Engineering for Women


TESTING LOOPS
While ($a! = $b)
if ($a > $b) {
$a=$a-$b
} else {
$b=$b-$a
}
}
• With the if statement, the expression that forms the condition must be
enclosed in brackets. But now, while can be replaced by until to give the same
effect. Single statement can use while and until as statement modifiers to
improve readability.
Eg: $a += 2 while $a <$b;
$a += 2 until $a > $b;

BVRIT HYDERABAD College of Engineering for Women


Counting Loops
• In Perl,
foreach $i (1…10),
$i_square=$i* $i;
$i_cube=$i**3;
print “$i\t$i_square\t$i_cube\n”;
}

BVRIT HYDERABAD College of Engineering for Women


LISTS
• A list is a collection of scalar data items which can be treated as a whole, and has a
temporary existence on the run-time stack.
• It is a collection of variables, constants (numbers or strings) or expressions, which is to
be treated as a whole. It is written as a comma-separated sequence of values, eg:
“red” , “green” , “blue”.
• A list often appears in a script enclosed in round brackets.
• For eg:( “red” , “green”, “blue” ) Shorthand notation is acceptable in lists, for eg:
(1..8)(“A”..”H” , “O”..”Z”) qw(the quick brown fox) is a shorthand for (“the” , ”quick” ,
”brown” , ”fox”).
• Arrays and Hashes: These are the collections of scalar data items which have an
assigned storage space in memory, and can therefore be accessed using a variable
name.

• Array in Perl is a variable that contains the list.


• A Simple list is one that contains homogeneous elements.

BVRIT HYDERABAD College of Engineering for Women


ARRAYS
• An array is an ordered collection of data whose comparisons are
identified by an ordinal index: It is usually the value of an array variable.
• The name of the variable always starts with an @, eg: @days_of_week.
• NOTE: An array stores a collection, and List is a collection, So it is
natural to assign a list to an array.
Eg: @rainfall = (1.2 , 0.4 , 0.3 , 0.1 , 0 , 0 , 0 );
• A list can occur as an element of another list.
Eg: @foo = (1 , 2 , 3, “string”);
@foobar = (4 , 5 , @foo , 6);
• The foobar result would be (4 , 5 , 1 , 2 , 3 , “string” , 6);

BVRIT HYDERABAD College of Engineering for Women


HASHES
• An associative array is one in which each element has two components
: a key and a value, the element being ‘indexed’ by its key.
• Such arrays are usually stored in a hash table to facilitate efficient
retrieval, and for this reason Perl uses the term hash for an associative
array.

• Names of hashes in Perl start with a % character: such a name


establishes a list context. The index is a string enclosed in braces(curly
brackets).

BVRIT HYDERABAD College of Engineering for Women


%data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
%data = ('John Paul' => 45, 'Lisa' => 30, 'Kumar' => 40);
%data = (-JohnPaul => 45, -Lisa => 30, -Kumar => 40);
Array Creation
• Array variables are prefixed with the @sign and are populated using
either parenthesis or the qw operator.
Eg: @array = (1 , 2 ,”Heelo”);
@array = qw/This is an array/;
• In C, C++, Java; Array is a collection of homogeneous elements,
whereas; In Perl, Array is collection of heterogeneous elements.

BVRIT HYDERABAD College of Engineering for Women


Accessing Array Elements
• When accessing an individual element, we have to use the ‘$’ symbol followed
by variable name along with the index in the square brackets.
Eg: $bar = $foo[2];
$foo[2] = 7;
• A group of contiguous elements is called a slice , and is accessed using a simple
syntax:
@foo[1..3] is the same as the list ($foo[1], $foo[2], $foo[3])
• A slice can be used as the destination of an assignment, Eg: @foo*1..3+ =
(“hop” , “skip” , “jump”);
• Like a slice, a selection can appear on the left of an assignment: this leads to a
useful idiom for rearranging the elements in a list.
Eg: To swap the first two elements of an array, we write as;
@foo[0 , 1] = @foo[1 , 0];

BVRIT HYDERABAD College of Engineering for Women


Manipulating Lists
• Perl provides several built-in functions for list manipulation.
• Three useful ones are:
• shift LIST : Returns the first item of LIST, and moves the remaining
items down, reducing the size of LIST by 1.
• unshift ARRAY, LIST : The opposite of shift. Puts the items in LIST
at the beginning of ARRAY, moving the original contents up by the
required amount.
• push ARRAY, LIST : Similar to unshift, but adds the values in LIST to
the end of ARRAY.

BVRIT HYDERABAD College of Engineering for Women


Iterating over Lists
• foreach: The foreach loop performs a simple iteration over all the
elements of a list.

Eg: @list = ( 20, 30, 40, 50);


foreach $a (@list)
{
print "value of a: $a\n";
}

BVRIT HYDERABAD College of Engineering for Women


Working With Hashes
• A hash is a set of key/value pairs.
• Hash variables are preceded by a “%” sign.
• To refer to a single element of a hash, you will use the hash variable
name preceded by a ‘$’ sign and followed by the “key” associated
with the value in the curly brackets.
• It is also called as associative array.

BVRIT HYDERABAD College of Engineering for Women


Creating Hashes
• We can assign a list of key-value pairs to a hash, as, for example
%foo = (key1, value1, key2, value2, …….);
• An alternative syntax is provided using the => operator to associate
key-value pairs, thus:%foo = (banana => ‘yellow’ , apple => ‘red’ ,
grapes => ‘green’, ............);

BVRIT HYDERABAD College of Engineering for Women


Manipulating Hashes
• Perl provides a number of built-in functions to facilitate manipulation
of hashes. If we have a hash called HASH
• keys % HASH returns a list of the keys of the elements in the hash,
and
• values % HASH returns a list of the values of the elements in the hash.

• Eg: %foo = (banana => ‘yellow’ , apple => ‘red’ , grapes => ‘green’,
............);
• keys % HASH returns banana, apple ,grapes
• values % HASH returns yellow, red, green.

BVRIT HYDERABAD College of Engineering for Women


• These functions provide a convenient way to iterate over the
elements of a hash using foreach:

foreach (keys % HASH)


{
process $magic($_)
}

• Other useful operators for manipulating hashes are delete and exists.
• delete $HASH{$key} removes the element
• exists $HASH{$key} returns true.

BVRIT HYDERABAD College of Engineering for Women


Strings, Pattern Matching & Regular
Expressions in Perl
• The most powerful features of Perl are in its vast collection of string
manipulation operators and functions.
• Perl would not be as popular as it is today in bioinformatics
applications if it did not contain its flexible and powerful string
manipulation capabilities.

https://www.tutorialspoint.com/perl/perl_regular_expressions.htm

BVRIT HYDERABAD College of Engineering for Women


String concatenation
• To concatenate two strings together, just use the ( .) dot
$a . $b;
$c = $a . $b; $a = $a . $b; $a .= $b;
• The first expression concatenates $a and $b together, but the the
result was immediately lost unless it is saved to the third string $c as
in case two.
• If $b is meant to be appended to the end of $a, use the .= operator
will be more convenient.
• As is any other assignments in Perl, if you see an assignment written
this way $a = $a op expression, where op stands for any operator and
expr stands for the rest of the statement, you can make a shorter
version by moving the op to the front of the assignment, e.g., $a op=
expression.
BVRIT HYDERABAD College of Engineering for Women
• Substring extraction
• The counterpart of string concatenation is substring extraction.
• To extract the substring at certain location inside a string, use the
substr function:
• $second_char = substr($a, 1, 1);
• $last_char = substr($a, -1, 1);
• $last_three_char = substr($a, -3);
• The first argument to the substr function is the source string, the
second argument is the start position of the substring in the source
string, and the third argument is the length of the substring to
extract.
• The second argument can be negative, and if that being the case, the
start position will be counted from the back of the source string. Also,
the third argument can be omitted.
BVRIT HYDERABAD College of Engineering for Women
• In that case, it will run to the end of the source string.
• Particularly interesting feature in Perl is that the substr function can
be assigned into as well, meaning that in addition to string extraction,
it can be used as string replacement:
• substr($a, 1, 1) = 'b'; # change the second character to b
• substr($a, -1) = 'abc'; # replace the last character as
• substr($a, 1, 0) = 'abc'; #insert abc in front of the second character

BVRIT HYDERABAD College of Engineering for Women


Substring search
• In order to provide the second argument to substr, usually you need
to locate the substring to be extracted or replaced first.
• The index function does the job:
o $loc1 = index($string, "abc");
o $loc2 = index($string, "abc", $loc+1);
o print "not found" if $loc2<0;
• The index function takes two arguments, the source string to search,
and the substring to be located inside the source string.
• It can optionally take a third argument to mean the start position of
the search.
• If the index function finds no substring in the source string anymore,
then it returns -1.

BVRIT HYDERABAD College of Engineering for Women


• The basic method for applying a regular expression is to use
the pattern binding operators =~ and !~.
• The first operator is a test and assignment operator.

• There are three regular expression operators within Perl.


• Match Regular Expression - m
• Substitute Regular Expression - s
• Transliterate Regular Expression - tr
• You can use any combination of naturally matching characters to act
as delimiters for the expression.

• For example, m{}, m(), and m>< are all valid.


• Regular expression variables include $, which contains whatever the last
grouping match matched;

• $&, which contains the entire matched string;

• $`, which contains everything before the matched string;

• and $', which contains everything after the matched string.

$f="foo";
$true = ($f =~ m/foo/);
print $true;
$true = ??
Replacement
Translation
Meta Character matching
Regular expression
• Regular expression is a way to write a pattern which describes certain
substrings. In general, the number of possible strings that can match a
pattern is large, thus you need to make use of the regular expression
to describe them instead of listing all possibilities.
• If the possible substring matches are just one, then maybe the index
function is more efficient.
• The following are some basic syntax rules of regular expression:
• Any character except the following special ones stands for itself. Thus abc
matches 'abc', and xyz matches 'xyz'.
• The character . matches any single character. To match it only with the .
character itself, put an escape \ in front of it, so \. will match only '.', but . will
match anything. To match the escape character itself, type two of them \\ to
escape itself.

BVRIT HYDERABAD College of Engineering for Women


• If instead of matching any character, you just want to match a subset
of characters, put all of them into brackets [ ], thus [abc] will match
'a', 'b', or 'c'. It is also possible to shorten the listing if characters in a
set are consecutive, so [a-z] will match all lowercase alphabets, [0-9]
will match all single digits, etc. A character set can be negated by the
special ^ character, thus [^0-9] will match anything but numbers, and
[^a-f] will match anything but 'a' through 'f'. Again, if you just want to
match the special symbols themselves, put an escape in front of
them, e.g., \[, \^ and \].
• All the above so far just match single characters. The power of regular
expression lies in its ability to match multiple characters with some
meta symbols. The * will match 0 or more of the previous symbol, the
+ will match 1 or more of the previous symbol, and ? will match 0 or 1
of the previous symbol.

BVRIT HYDERABAD College of Engineering for Women


• For example, a* will match 'aaaa...' for any number of a's including
none '', a+ will match 1 or more a's, and a? will match zero or one a's.
A more complicated example is to match numbers, which can be
written this way [0-9]+. To matching real numbers, you need to write
[0-9]+\.?[0-9]*. Note that the decimal point and fraction numbers can
be omitted, thus we use ?, and * instead of +.
• o If you want to combine two regular expressions together, just write
them consecutively. If you want to use either one of the two regular
expressions, use the | meta symbol. Thus, a|b will match a or b,
which is equivalent to [ab], and a+|b+ will match any string of a's or
b's. The second case cannot be expressed using character subset
because [ab]+ does not mean the same thing as a+|b+.

BVRIT HYDERABAD College of Engineering for Women


• Finally, regular expressions can be grouped together with
parentheses to change the order of their interpretation. For
example, a(b|c)d will match 'abd' or 'acd'. Without the parentheses,
it would match 'ab' or 'cd'.
• The rules above are simple, but it takes some experience to apply them
successfully on the actual substrings you wish to match.
• There are no better ways to learn this than simply to write some
regular expressions and see if they match the substrings you have in
mind.

BVRIT HYDERABAD College of Engineering for Women


• The following are some examples:
• [A-Z][a-z]* will match all words whose first character are capitalized
• [A-Za-z_][A-Za-z0-9_]* will match all legal perl variable names
• [+-]?[0-9]+\.?[0-9]*([eE][+-]?[0-9]+)? will match scientific numbers
• [acgtACGT]+ will match all DNA strings
• ^> will match the > symbol only at the beginning of a string
• a$ will match the a letter only at the end of a string
• In the last two examples above, we introduced another two special
symbols.
• The ^ which when not used inside a character set to negate the
character set, stands for the beginning of the string.
• Thus, ^> will match '>' only when it is the first character of the string.
• Similarly, $ inside a regular expression means the end of the string, so
a$ will match 'a' only when it is the last character of the string. These
are so called anchor symbols.

BVRIT HYDERABAD College of Engineering for Women


• Another commonly used anchor is \b which stands for the boundary
of a word.
• In addition, Perl introduces predefined character sets for some
commonly used patterns, thus \d stands for digits and is equivalent to
[0-9], \w stands for word letters or numbers, and \s stands for space
characters ' ', \t, \n, \r, etc.
• The capital letter version of these negates their meaning, thus \D
matches non-digit characters, \W matches non-word characters, and
\S matches non- whitespaces.
• The scientific number pattern above can therefore be rewritten as:
[+-]?\d+\.?\d*([eE][+-]?\d+)?

BVRIT HYDERABAD College of Engineering for Women


GREP -global regular expression print

\ Quote the next metacharacter


^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping

[] Character class
Pattern matching

if ($string =~ / \d+ /)
{
print "there are numbers in $string\n";
}

if ($string =~ /(\d+)\s+(\d+)\s+(\d+)/)
{
print "first three matched numbers are $1, $2, $3 in $string\n";
}

BVRIT HYDERABAD College of Engineering for Women


Find the result
$string= "1 2 3 4 5";

if ($string =~ /(\d+)\s+(\d+)\s+(\d+)/)
{
print "first three matched numbers are $1, $2, $3 in $string\n";
}

else
{
print " no numbers ";
}

first three matched numbers are 1, 2, 3 in 1 2 3 4 5


$string= "1 2 3 2 5";

if ($string =~ /(\d).+\1/)
{
print "$1 happened at least twice in $string\n";
}

2 happened at least twice in 1 2 3 2 5


$string =~ s/\d+/0/; # replace a number with zero

$string =~ s:/:\\:; # replace the forward slash with backward slash

$string =~ s/(\d+)/(\1)/; - add parentheses around the matched number


• /g tells Perl to match all existing patterns, thus the following prints all
numbers in
$string
while ($string =~ /(\d+)/g)
{
print "$1\n";
}

$string =~ /abc/i; # matches AbC, abC, Abc, etc.


• Modifiers to pattern matching and substitution
• You can add some suffix modifiers to Perl pattern matching or substitution
operators to tell them more precisely what you intend to do:
• /g tells Perl to match all existing patterns, thus the following prints all
numbers in
$string
while ($string =~ /(\d+)/g)
{
print "$1\n";
}
• $string =~ s/\d+/0/g; # replace all numbers in $string with zero /i tells Perl
to ignore cases, thus
$string =~ /abc/i; # matches AbC, abC, Abc, etc.
• /m tells perl to ignore newlines, thus
"a\na\na" =~ /a$/m will match the last a in the $string, not the a before
the first newline if /m is not given.

BVRIT HYDERABAD College of Engineering for Women


Perl- Subroutines
• A Perl subroutine or function is a group of statements that together
performs a task. You can divideup your code into separate subroutines.
• How you divide up your code among different subroutines is up to you, but
logically the division usually is so each function performs a specific task.
Define and Call a Subroutine
• The general form of a subroutine definition in Perl programming language is
as follows –
sub subroutine_name
{
body of the subroutine
}

BVRIT HYDERABAD College of Engineering for Women


• The typical way of calling that Perl subroutine is as follows –
subroutine_nam e( list of argum ents );Let's have a look into the
following example, which defines a simple function and then call it.
Because Perl compiles your program before executing it, it doesn't
matter where you declare your subroutine.
#!/usr/bin/perl
# Function definition
sub Hello{
print "Hello, World!\n";
}
# Function call Hello();
• When above program is executed, it produces the following result −
Hello, World!

BVRIT HYDERABAD College of Engineering for Women


Passing Arguments to a Subroutine
• You can pass various arguments to a subroutine like you do in any
other programming language and they can be accessed inside the
function using the special array @_. Thus the first argument to the
function is in [ 0], this concision_[1], and so on.
• You can pass arrays and hashes as arguments like any scalar but
passing more than one array or hash normally causes them to lose
their separate identities.

BVRIT HYDERABAD College of Engineering for Women


With and without arguments

sub area
sub hello {
{ $length = $_[0];
print “Welcome All !\n"; $breadth= $_[1];
} print ($length * $breadth);
hello(); }
area(5,4);
Passing Lists to Subroutines #!/usr/bin/perl
• Because the @_ variable is an # Function definition
array, it can be used to supply sub PrintList{
lists to a subroutine. m y @ list = @ _;
• However, because of the way in print "Given list is @ list\n";
which Perl accepts and parses }
lists and arrays, it can be difficult $ a = 10;
to extract the individual @ b = (1, 2, 3, 4);
elements from @_.
# Function call with list parameter
• If you have to pass a list along PrintList($ a, @ b);
with other scalar arguments,
then make list as the last
When above program is executed, it
argument as shown below −
produces the following result − Given
list is 10 1 2 3 4
BVRIT HYDERABAD College of Engineering for Women
Passing Lists to Subroutines

sub PrintList
{
@list = @ _;
print "Given list is @list\n";
}
$ a = 10;
@ b = (1, 2, 3, 4);
PrintList($ a, @ b);
Passing Hashes to Subroutines
• When you supply a hash to a subroutine or operator that accepts a list, then hash
is automatically translated into a list of key/value pairs.

sub PrintHash
{
%hash = @_;
foreach $key (keys %hash )
{
$value = $hash{$key};
print "$key : $value\n";
}
}
%hash = ('name' => 'Tom', 'age' => 19);

PrintHash(%hash);

BVRIT HYDERABAD College of Engineering for Women


Returning Value from a Subroutine
• You can return a value from subroutine like you do in any other
programming language.

• If you are not returning a value from a subroutine then whatever


calculation is last performed in a subroutine is automatically also the
return value.

• You can return arrays and hashes from the subroutine like any scalar
but returning more than one array or hash normally causes them to
lose their separate identities.

BVRIT HYDERABAD College of Engineering for Women


sub area
{
# passing argument
$side = $_[0];

return ($side * $side);


}

area (4)

BVRIT HYDERABAD College of Engineering for Women


Lab programs
$1 .. $9 are also read only variables so you can't implicitly assign a value to them

These are called "match variables". They contain the text from last
regular expression match.

special variables $1, $2, …, $9, where $1 contains the substring


matched the first pair of parentheses, and so on .

$1foo = 'foo’;
print $1foo; // This causes error .
Thank You

You might also like