24
CHARACTERISTICS OF SCRIPTING LANGUAGE Efficiency is not an issue Ease of use is achieved at the expense of efficiency eg: interpretation rather than compiling Focus is not on high performance but on the speed of development together with ability to make changes to meet new requirement. 6/13/2016 1 Introduction to scripts and scripting

Unit 1-introduction to perl

Embed Size (px)

Citation preview

Page 1: Unit 1-introduction to perl

CHARACTERISTICS OF SCRIPTING LANGUAGE Efficiency is not an issue

Ease of use is achieved at the expense of efficiencyeg: interpretation rather than compilingFocus is not on high performance but on the speed of development together with ability to make changes to meet new requirement.

6/13/2016 1Introduction to scripts and scripting

Page 2: Unit 1-introduction to perl

2

INTRODUCTION TO PERLBy Sana Mateen

6/15/2016

Page 3: Unit 1-introduction to perl

3

PERL—PRACTICAL EXTRACTION REPORT LANGUAGE Perl is a programming language developed by Larry Wall, especially

designed for text processing. Though Perl is not officially an acronym but many times it is used as 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 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(wxperl and perl-tk interfaces), and more.

6/15/2016

Page 4: Unit 1-introduction to perl

4

WHAT IS PERL? AND PERL FEATURES ?1. Perl is a stable, cross platform programming language.2. Though Perl is not officially an acronym but few people used it as Practical

Extraction and Report Language.3. It is used for mission critical projects in the public and private sectors.4. Perl is an Open Source software, licensed under its Artistic License, or the GNU

General Public License (GPL).5. Perl takes the best features from other languages, such as C, awk, sed, sh, and

BASIC, among others. Features:1. Perls database integration interface DBI supports third-party databases including

Oracle, Sybase, Postgres, MySQL and others.2. Perl works with HTML, XML, and other mark-up languages.3. Perl supports Unicode.4. Perl is Y2K compliant.5. Perl supports both procedural and object-oriented programming.6. Perl interfaces with external C/C++ libraries through XS or SWIG.7. Perl is extensible. There are over 20,000 third party modules available from the

Comprehensive Perl Archive Network (CPAN).

6/15/2016

Page 5: Unit 1-introduction to perl

5

PERL AND THE WEB

Perl used to be the most popular web programming language due to its text manipulation capabilities and rapid development cycle.

Perl is widely known as " the duct-tape of the Internet". Perl can handle encrypted Web data, including e-commerce transactions. Perl can be embedded into web servers to speed up processing by as much

as 2000%. Perl's mod_perl allows the Apache web server to embed a Perl interpreter. Perl's DBI package makes web-database integration easy.

6/15/2016

Page 6: Unit 1-introduction to perl

6

PERL IS INTERPRETED

Perl is an interpreted language, which means that your code can be run as is, without a compilation stage that creates a non portable executable program.

Traditional compilers convert programs into machine language. When you run a Perl program, it's first compiled into a byte code, which is then converted ( as the program runs) into machine instructions. So it is not quite the same as shells, or Tcl, which are strictly interpreted without an intermediate representation.

It is also not like most versions of C or C++, which are compiled directly into a machine dependent format.

6/15/2016

Page 7: Unit 1-introduction to perl

7

PERL - ENVIRONMENT SETUPThere is a set up of Perl Programming environment online.

Before we start writing our Perl programs, let's understand how to setup our Perl environment. Perl is available on a wide variety of platforms −•Unix•Win 9x/NT/2000/ •WinCE •Macintosh (PPC, 68K) •Solaris (x86, SPARC) •OpenVMS •Symbian •And many more... This is more likely that your system will have perl installed on it. Just try giving the following command at the $ prompt −$perl -v If you have perl installed on your machine then you will get a message something as follows −

6/15/2016

Page 8: Unit 1-introduction to perl

8

Getting Perl Installation The most up-to-date and current source code, binaries, documentation, news, etc. are available at

the official website of Perl. Perl Official Website − http://www.perl.org/ Install Perl Perl distribution is available for a wide variety of platforms. You need to download only the

binary code applicable for your platform and install Perl. If the binary code for your platform is not available, you need a C compiler to compile the source

code manually. Compiling the source code offers more flexibility in terms of choice of features that you require in your installation.

Here is a quick overview of installing Perl on various platforms. 1) Unix and Linux Installation Here are the simple steps to install Perl on Unix/Linux machine. Open a Web browser and go to http://www.perl.org/get.html. Follow the link to download zipped source code available for Unix/Linux.

6/15/2016

Page 9: Unit 1-introduction to perl

9

oDownload perl-5.x.y.tar.gz file and issue the following commands at $ prompt.

oNOTE − Here $ is a Unix prompt where you type your command, so make sure you are not typing $ while typing the above mentioned commands.oThis will install Perl in a standard location o/usr/local/bin and oits libraries are installed in /usr/local/lib/perlXX, where XX is the version of Perl that you are using.oIt will take a while to compile the source code after issuing the make command.

oOnce installation is done, you can issue perl -v command at $ prompt to check perl installation. If everything is fine, then it will display details of perl .

2) Windows InstallationoHere are the steps to install Perl on Windows machine.oFollow the link for the Strawberry Perl installation on Windows http://strawberryperl.comoDownload either 32bit or 64bit version of installation.oRun the downloaded file by double-clicking it in Windows Explorer. This brings up the Perl install wizard, which is really easy to use. Just accept the default settings, wait until the installation is finished, and you're ready to roll!

6/15/2016

Page 10: Unit 1-introduction to perl

10

RUNNING A PERL These are the different ways to start Perl. (1) Interactive Interpreter You can enter perl and start coding right away in the interactive interpreter

by starting it from the command line. You can do this from Unix, DOS, or any other system, which provides you a command-line interpreter or shell window.

-e option ------ Runs Perl script sent in as program

6/15/2016

Page 11: Unit 1-introduction to perl

11

(2) Script from the Command-lineA Perl script is a text file which keeps perl code in it and it can be executed at the command line by invoking the interpreter on your application, as in the following

(3) Integrated Development EnvironmentYou can run Perl from a graphical user interface (GUI) environment as well. All you need is a GUI application on your system that supports Perl. You can download Padre, the Perl IDE. You can also use Eclipse Plugin EPIC - Perl Editor and IDE for Eclipse if you are familiar with Eclipse.

6/15/2016

Page 12: Unit 1-introduction to perl

12

NAMES IN PERL Perl manipulates variables which have a name. A value is assigned to/stored in variable by assignment statement of the

formname=value

Perl distinguishes between singular name and plural name.A singular name –holds single item of data– scalar valueA plural name for variable – hold collection of data items —an array or hash

Starting special character of variable denotes the kind of thing that name stands for

$ ---- Scalar data@ ----- Array% ----- Hash& ----- Sub routine

6/15/2016

Page 13: Unit 1-introduction to perl

13

NAMES IN PERL...

use strict ‘var’; (or) use strict; It tells perl to insist on declaration by placing the line. At the start of script variables are declared using my $x,$y;

#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] =

$names[2]\n";

6/15/2016

Page 14: Unit 1-introduction to perl

14

NAMES IN PERL... Valid characters are letters,digits,underscores. First character after special character can be a letter or underscore. Names may also have non-alphanumeric character after special character.

$$,$? (system reserved names in Perl ) Each kind of data has separate namespace. Special character determine the context in which the name is being used. In C language a new variable is declared as

int i=1;float data[9];

Scope of variable depends on the part of program in which the variable is visible and available for use.

Global scope and local scope. Variable declaration in perl –

$a=5;my $a=10;

A variable comes into existence when declared or first used with special value denoted by undef

undef $x;

6/15/2016

Page 15: Unit 1-introduction to perl

15

PERL NAMES,VALUES AND VARIABLESBYSANA MATEEN

6/18/2016

Page 16: Unit 1-introduction to perl

NAMES IN PERL Perl manipulates variables which have a name. A value is assigned to/stored in variable by assignment statement of the

formname=value

Perl distinguishes between singular name and plural name.A singular name –holds single item of data– scalar valueA plural name for variable – hold collection of data items —an array or hash

Starting special character of variable denotes the kind of thing that name stands for

$ ---- Scalar data@ ----- Array% ----- Hash& ----- Sub routine

6/18/2016

16

Page 17: Unit 1-introduction to perl

NAMES IN PERL... Valid characters are letters,digits,underscores. First character after special character can be a letter or underscore. Names may also have non-alphanumeric character after special character.

$$,$? (system reserved names in Perl ) Each kind of data has separate namespace. Special character determine the context in which the name is being used. In C language a new variable is declared as

int i=1;float data[9];

Scope of variable depends on the part of program in which the variable is visible and available for use.

Global scope and local scope. Variable declaration in perl –

$a=5;my $a=10;

A variable comes into existence when declared or first used with special value denoted by undef

undef $x;

6/18/2016

17

Page 18: Unit 1-introduction to perl

NAMES IN PERL...

use strict ‘var’; (or) use strict; It tells perl to insist on declaration by placing the line. At the start of script variables are declared using my $x,$y;

#!/usr/bin/perl @ages = (25, 30, 40); @names = ("John Paul", "Lisa", "Kumar"); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] =

$names[2]\n";

6/18/2016

18

Page 19: Unit 1-introduction to perl

19

A scalar is a single unit of data. Perl recognizes two kinds of scalar data , a String and Numbers . There’s no difference between integers and real numbers both are same.Here is a simple example of using scalar variables −

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

This will produce the following result −Age = 25 Name = John Paul Salary = 1445.5

Strings are stored as sequence of bytes of unlimited length . Perl is dynamically typed language (System keeps track of whether a variable contains a numeric value or string value).Depending on the context strings are converted to int.Eg:If int/num occurs in String context, operand for string operator , perl will convert it to string

Numeric ScalarsA scalar is most often either a number or a string. Following example demonstrates the usage of various types of numeric scalars −

6/18/2016

Page 20: Unit 1-introduction to perl

20

#!/usr/bin/perl $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";

This will produce the following result −integer = 200 negative = -300 floating = 200.34 bigfloat = -1.2e-23 octal = 255 hexa = 255

String ScalarsFollowing example demonstrates the usage of various types of string scalars. Notice the difference between single quoted strings and double quoted strings −#!/usr/bin/perl $var = "This is string scalar!"; $quote = 'I m inside single quote - $var'; $double = "This is inside single quote - $var"; $escape = "This example of escape -\tHello, World!"; print "var = $var\n"; print "quote = $quote\n"; print "double = $double\n"; print "escape = $escape\n";

6/18/2016

Page 21: Unit 1-introduction to perl

21

STRING CONSTANTS/LITERALS String constant and literals can be enclosed in single or double quotes. The string is terminated by first next occurrence of quote which started it , so single

quoted strings can include double quotes and vice versa. Single quoted strings are treated as it is-

‘Friday\n’ ‘Friday’--- String ‘Friday\n’---String with seven characters including last character which is a new

line. \n-newline,\t-tab,\U-uppercase There is more than one way to choose your own quote 1.quote — q 2.double quote– qq q /any string/ or q(any string) and qq(any string), qq /any string/

6/18/2016

Page 22: Unit 1-introduction to perl

22

VARIABLES AND ASSIGNMENT Perl uses – ‘=‘ as the assignment operator. It returns a value. This permits

statement like $b=4+($a=3); $a=“Burger”; $b=“Sandwich $a” //$b would give “Sandwich Burger” $c=“turkey $a”; Scalar variable names start with--$ $a=“java”; $b=“${a} script”;//value is javascript

6/18/2016

Page 23: Unit 1-introduction to perl

23

<STDIN> <STDIN>is used for acquiring input from keyboard.If no input is queued

perl will wait until a line is typed and the return key pressed. End-of-file

ctrl - D Unixctrl - Z DOS

They cause the return to be undefined, it evaluates to “ “ . The empty string is treated as false in boolean context.

while(<STDIN>){.....}To process all statements until the end of file is reached.While(defined <STDIN>){...}

6/18/2016

Page 24: Unit 1-introduction to perl

BYSANA MATEEN

SCALAR EXPRESSIONS AND CONTROL STRUCTURES