my perl ppt

Post on 18-Aug-2015

24 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

PERL

PRESENTED BY: P.SAI NITHISH KUMAR

INSTRUCTOR: DR. ANDREWS FIGUEROA.

Index Introduction to perl

Why perl?

Perl environment

Installation procedure

Execution procedure

First line

Perl basics

Evaluation of perl

Datatypes

Functions

Mistakes done by programmer

Advantages

Disadvantages

INTRODUCTION

Its written by Larry wall and first released in 1987.

Perl is an Open Source Software. Hence easy available.

Perl in an interpreted language, like Java, Pascal, awk, sed, Tcl, or Smalltalk.

It is a scripting language .

Perl is a case sensitive programming language.

Why PERL ?

Perl stands for practical extraction and report language. It is ideally used to handle words and text.

Easy available. All the data is available in internet Perl is free to download from the GNU website so it is very easily

accessible . Perl is also available for MS-DOS,WIN-NT, Windows OS etc. Perl language is quoted as “it makes an easy task even easier

and makes the harder things possible”

Perl Environment

Perl is available on a wide variety of platforms:

Unix (Solaris, SunOS, etc..)

Linux(Redhat, Ubuntu, Fedora etc.)

Windows

MacCan use system commands

Symbian

Debian GNU/kFreeBSD

MirOS BSD

And many more…

Perl features:

Perl takes the best features from other languages, such as C, awk, sed, sh, and BASIC, among other

Perl works with HTML, XML, and other mark-up languages.

Perl supports Unicode.

Perl supports both procedural and object-oriented programming.

The Perl interpreter can be embedded into other systems, which means that your code can be run as it is, without a compilation stage that creates a non portable executable program.

Perl Basics

Statements must end with semicolon $a = 0;

Should call exit() function when finished Exit value of zero means success

exit (0); # successful

Exit value non-zero means failure exit (2); # failure

Basic input/output files:

Predefined File Handles <STDIN> input

<STDOUT> output

<STDERR> output

print STDERR “big bad error occurred\n”;

Versions of Perl:

5.5 2004-02-23

5.6 2003-11-15

5.8 2008-12-14

5.10 2009-08-23

5.12 2012-11-10

5.14 2013-03-10

5.16 2013-03-11

5.18 2014-10-02

5.20 2015-02-14

5.22 2015-06-01

PERL Installation Procedure

Linux $tar –xzf perl-5.x.y.tar.gz

$cd perl-5.x.y

$./Configure –de

$make

$make test

$make install

Windows Install software like strawberry

Perl

http://activestate.com/

Also check out these sites: ● http://perl.com/ http://www.perl.org/

Execution Procedure :

The Perl scripts should be saved in .pl extension

Example:-

Helloworld.pl

Execution:

$perl script.pl --- for Linux/Unix

C:>perl script.pl --- for Windows/MS-DOS

First line

In linux and unix platform we have to use the line given below before entering the script and windows doesn’t need any thing.

• #!/user/bin/perl //it gives the path to the program

Here, /user/bin/perl is actual Perl interpreter binary.

# is used for comment..

Example:

$perl -e 'print "Hello World\n“’;

Output: hello, world

Write a script:

We generally start a Perl script with the "shebang" line,

which looks something like one of these:

#!/usr/bin/perl

#!/usr/local/bin/perl

#!/usr/local/bin/perl5

#!/usr/bin/perl -w etc

● This is not necessary on many systems, but when it is, it is, so it's a good habit to get into.

● The purpose of this line is to tell the server which version of Perl to use, by pointing to the location in the server directory of the Perl executable.

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

Example: @abc=10;

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

Example:

$Manpower and $manpower are two different identifiers in Perl.

EVALUATION OF PERL: Readability:

Perl programming language is very hard to read which means it has poor readability because it contains lot of symbols like $,#,/ etc reading these all and remembering all these is quite difficult process. Hence PERL language has poor readability.

Writability:

Perl is a very forgiving language that allows for multiple forms of expression. That means that you can say something in various ways, as long as your syntax is correct.

Simple English language is used to develop a perl code

Hence, writable.

Cost:

As this language is free available or an open source language, it is cost efficient.

Reliability:

As Perl is available on all platform it works on all coditions hence it is reliable.

DATA TYPES:

Perl does not specify the types of variable.

It is loosely typed language. Where languages like c and java are strongly typed.

Types and Description:

Scalar − Scalars are simple variables. They are preceded by a dollar sign ($).

$xyz=20; 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. A file handle is used for reading the binary data from

a file into scalar variable.

Arrays − 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 −Hashes are unordered sets of key/value pairs that you access using the keys as subscripts. They are preceded by a percent sign (%).

Data types Examples:

Here's an example: (Note scalar variables begin with an $)

$a="5.0"; # set up our variables

$b="5"; # # to the end of a line is a comment in perl

print "Are these variables equal as numbers? ",$a==$b,"\n";

print "Are the variables equal as strings? ",$a eq $b,"\n";

print "These variables are equal as strings\n" if($a eq $b);

print "These variables are equal numerically\n" if($a==$b);

VARIABLES:

List (one-dimensional array)

@memory = (16, 32, 48, 64); @people = (“Alice”, “Alex”, “Albert”); First element numbered 0 Single elements are scalar: $names[0] = “Fred”; How big is my list?

print “Number of people: “, scalar @people, “ \n”; (Cont..)

VARIABLES:

Hash (associative array) %var = (‘Nithish', 24, 'Kumar', 20);

print “\$data{‘nithish} = $data{‘nithish'}\n";

print "\$data{'Kumar'} = $data{'Kumar'}\n";

Single elements are scalar print $var{“name”}; $var{age}++;

How many elements are in my hash? @allkeys = keys(%var);

$num = @allkeys;

Variable interpolation:

It is the main feature in perl

Variable names are automatically replaced by the values whenever they appear in double coated strings.

Example:

$stud = “nithish”;

$marks = 95;

print “mark obtained by $stud is $marks\n”;

print ’marks obtained by $stud is $marks\n’;

OUTPUT:

Marks obtained by Nithish is 95

Marks obtained by $stud is $mark

Special Variables:

$& Stores the value which matched with pattern.

$_ The default input and pattern-searching space.

$' Stores the value which came after the pattern in the linevalue.

$” Stores the value which came before the pattern in the linevalue.

OPERATORS:

Math The usual suspects: + - * / %

$total = $subtotal * (1 + $tax / 100.0);

Exponentiation: ** $cube = $value ** 3;

$cuberoot = $value ** (1.0/3);

Bit-level Operations left-shift: << $val = $bits << 1;

right-shift: >> $val = $bits >> 8; (Cont..)

OPERATORS:

Assignments As usual: = += -= *= /= **= <<= >>=

$value *= 5;

$longword <<= 16;

Increment: ++

$counter++ ++$counter

Decrement: --

$num_tries-- --$num_tries

Logical (expressions) && And operator

| | Or operator

! Not operator (Cont..)

OPERATORS:

Perl operators are the same as in C and Java these are only good for numbers, but beware:

$b = "3" + "5";print $b, "\n"; # prints the number 8

if a string can be interpreted as a number given arithmetic operators, it will be

what is the value of $b?:$b = "3" + "five" + 6?

Perl semantics can be tricky to completely

understand (Cont..)

OPERATORS:

Boolean (against bits in each byte)

Usual operators: & |

Exclusive-or: ^

Bitwise Negation: ~

$picture = $backgnd & ~$mask | $image;

Boolean Assignment &= |= ^=

$picture &= $mask;

CONTROL STAMENTS:(if)

#!/usr/local/bin/perl

$a = 10;

# check the boolean condition using if statement

if( $a < 20 )

{

# if condition is true then print the following

printf "a is less than 20\n";

}

print "value of a is : $a\n";

CONTROL STATEMENTS: (if)

“if” statement - first style

if ($porridge_temp < 40) { print “too hot.\n”;

}elsif ($porridge_temp > 150) {

print “too cold.\n”;}else {

print “just right\n”;

(Cont..)

CONTROL STAMENTS:(if)

“if” statement - second style statement if condition;

print “\$index is $index” if $DEBUG;

Single statements only

Simple expressions only

“unless” is a reverse “if” statement unless condition;

print “millenium is here!” unless $year < 2000;

Looping STATEMENT:(while)

“while” loop

• while (condition) { code }

• #!/usr/local/bin/perl $a = 10;

• # while loop execution

• while( $a < 20 )

{

•printf "Value of a: $a\n";

• $a = $a + 1;

}

o/p: Value of a: 10…….Value of a: 19

LOOPING STATEMENTS: (for)

This statement takes a list of values and assigns them one at a time to a scalar variable, executing a block of code with each successive assignment. Eg: Foreach $var (list) {}.

for” loop - first style for (initial; condition; increment) { code }

for ($i=0; $i<10; $i++) {print “hello\n”;

} “for” loop - second style

for [variable] (range) { code }

for $name (@employees) {print “$name is an employee.\n”;

}

SWITCH STATEMENTS:

Perl needs no Switch (Case) statement.

Use if/else combinations instead

if (cond1) { … }elsif (cond2) { … }elsif…else…

This will be optimized at compile time

FUNCTIONS:

Calling a Subroutine(function) &subname; # no args, return value

&subname (args);

retval = &subname (args);

The “&” is optional so long as… subname is not a reserved word

subroutine was defined before being called

(Cont..)

FUNCTIONS:

Passing Arguments(parameter) Passes the value

Lists are expanded @a = (5,10,15);

@b = (20,25);&mysub(@a,@b);

this passes five arguments: 5,10,15,20,25

mysub can receive them as 5 scalars, or one array

(Cont..)

FUNCTIONS: (example1)

Examples sub good1 {

my($a,$b,$c) = @_;}&good1 (@triplet);

sub good2 { my(@a) = @_;}&good2 ($one, $two, $three);

(Cont..)

FUNCTIONS: (example2)

Examples sub good3 {

my($a,$b,@c) = @_;}&good3 ($name, $phone, @address);

sub bad1 { my(@a,$b) = @_;}

@a will absorb all args, $b will have nothing.

(Cont..)

Split and Join

Join does the exact opposite job as that of the split.

It takes a list and joins up all its values into a single scalar variable using the delimiter provided.

Eg $newlinevalue = join(@data);

Operator precedence of perl:Associativity Operators

left Terms and list operators (leftward)

left ->nonassoc ++ --

right **right ! ~ n + - (unary)left =~ !~left * / % xleft + - .left << >>

nonassoc named unary operatorsnonassoc < > <= >= lt gt le genonassoc == != <=> eq ne cmp

left &left |^left &&left ||

nonassoc .. ...right ?:

right = += -= *= etc. (assignment operators)

left , =>nonassoc List operators (rightward)

right notleft andleft or xor

Previous

Common mistakes done:

putting comma after filehandle in print statement using == instead of eq, and != instead of ne leaving $ off the front of a variable on th left side of an

assignment forgetting the & on a subroutine call leaving $ off of the loop variable of foreach using else if or elif instead of elsif forgetting trailing semicolon forgetting the @ or @ on the front of variables saying @foo[1] when you mean $foo[1]

ADVANTAGES In PERL, one can use system commands

Quick scripts, complex scripts

Parsing & restructuring data files

High-level programming Networking libraries

Graphics libraries

Database interface libraries

We can print stuff on screen

Perl does not require you to treat everything as an object.Perl does not force you to use inheritance as the main mechanism for code reuse.Perl makes using composition for code reuse very straightforward.Perl's lexical scope and closures facilitate encapsulation.Perl allows multiple inheritance.

DISADVANTAGES:

It has poor readability

Impossible to maintain the code

Sytax of perl has less importance. Hence, it is non- trivial.

This programming language is overloaded.

Questions: What is perl?

a)Scripting language

b)Report language

c)a&b

d)None

What is file handle used for?

a)Reading the binary data from a file into scalar variable.

b) Finding where the file is on the disc

c)Deleting or moving the data

Perl was first invented in?

a) 1987

b) 1990

c) 1989

d) 1999

which of the following is used in perl?

a)Else if

b)Elseif

c)Elsif

d)elif

When you create a variable,you may assume it starts off containing :

a) 1

b) The Boolean values “false”

c) A null string (0 arithmetically)

Thank you

top related