Vous êtes sur la page 1sur 24

P E R LA n: I n tr o d u c tio n

Welcome
A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Questions & Doubts at the end please..........

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

PERL
An In t r o d u c t io
* A Crash Course for the uninitiated.
A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

History & Purpose

Larry Wall : Lazy programmer overkills solution for report generation from Usenet-news-like hierarchy of files for a bug-reporting system when awk runs out of steam. After some more additions and .... Perl is released to the Usenet community where people give more feedback and join him in developing Perl ( or perl or PERL or ..... , Practical Extraction & Reporting Language and yet still Pathologically Eclectic Rubbish Lister, so really whats in a name). Perl grew (and still is growing at version 6) exponentially like the *x OS in features and portability to get ......... Perl is designed to assist the programmer with common tasks that are probably too heavy or too portability-sensitive for the shell, and yet too weird or short-lived or complicated to code in C or some other *x glue language. highly portable and readily available Can be write-only Simple yet rich : There's more than one way to do it. (TMTOWTDI : Tim-Toady) ( The PERL Slogan. )

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
perl <scriptname> #!/usr/bin/env perl

Basic Execution Step

-------------------------------------------------------------#!/usr/bin/env perl print ("Hello, world!\n"); --------------------------------------------------------------------------------------------------------------------------#!/usr/bin/env perl print ("Enter your name : "); $Name = <STDIN> chomp ($Name); print ("Hello $name. Welcome to Perl. "); --------------------------------------------------------------

Hello World Example :

Little Better : A program to greet a User Entered name.

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

An Introduction to sca

Of Numerical data : Internally , Perl computes only with double-precision floating-point values. This means that there are no integer values internal to Perl; an integer constant in the program is treated as the equivalent floating-point value. Float Literals

-------------------------------------------------------------1.25 # about 1 and a quarter th -6.5e24 # negative 6.5 times 10 to the 24 (a "big" negative number) th -12e-24 # negative 12 times 10 to the -24 (a very small negative number) --------------------------------------------------------------

Integer Literals
-------------------------------------------------------------12 -2004 0377 # 377 octal, same as 255 decimal -0xff # negative FF hex, same as -255 decimal -------------------------------------------------------------A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Of Strings :

An Introduction to sca

-------------------------------------------------------------'hello' # five characters: h, e, l, l, o 'don\'t' # five characters: d, o, n, single-quote, t '' # the null string (no characters) 'hello\\n' # what have we here -------------------------------------------------------------

Single Quote (') strings: No Interpolation

-------------------------------------------------------------"hello world\n" # hello world and a newline "new \177" # new, space and the delete character (octal 177) "coke\tsprite" # coke,a tab and sprite -------------------------------------------------------------

Double Quotes () strings: Variable & \ Interpolation

Back tick (`) strings : way to run external commands and get back their output.

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Numerical Operators :

An Introduction to sca

Mathematical : /, *, +, - and exponentiation (**) and modulus (%) Logical Comparison : <, >, <=, >=, ==, != Binary Assignment : /=,*=,+=, -=, %= & even **= Auto Increment & Decrement : ++, -- (Post & Pre) String Operators : Concatenation : The dot (.) operator [ also can be used as .= ] Logical Comparison : lt, gt, lte, gte, eq, ne String Repetition : The single lowercase letter x (x)

-------------------------------------------------------------"something_" x 3 # is "something_something_something_" "something_else_" x (2+1) # is "something_else_" x 3, or # "something_else_something_else_something_else_" (3+2) x 4 # is 5 x 4, or really "5" x 4, which is "5555" --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

An Introduction to sca

Operator Precedence

Literal Conversion : If a string value is used as an operand for a numeric operator (say, +), Perl automatically converts the string to its equivalent numeric value, as if it had been entered as a decimal floating-point value. Trailing non numerics and leading whitespace are politely and quietly ignored, so " 123.45fred" (with a leading space) converts to 123.45 with nary a warning.[8] At the extreme end of this, something that isn't a number at all converts to zero without warning (such as the string Text used as a number).

Hex and octal values are not supported in this automatic conversion. Use hex and oct to interpret hex and octal values.

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

An Introduction to sca

Of chop() and chomp() chop() returns the discarded character after removing it from the passed argument chomp() returns the number of characters discarded after removing only the trailing newline characters (, or whatever the input record separator $/ is set to.)
-------------------------------------------------------------$a = "XYZ"; $b = "some text $a"; # $b is now "some text XYZ" $c = "no such variable $what"; # $c is "no such variable " $x = '$ABC'; # literally a dollar sign followed by "ABC" $y = "hey $ABC"; # value is 'hey $ABC': no double substitution $i = 'hi'; $j = "a test of " . '$i'; # literally: 'a test of $i' $k = "a test of \$i"; # same thing --------------------------------------------------------------

Interpolation of Scalars into Strings

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

An Introduction to sca

A little more on Interpolation of Scalars into Strings

-------------------------------------------------------------$Pay = "Pay"; $Payday = "wrong!"; $Text = "It's $Payday"; # not Payday, but "It's wrong!" $Text = "It's ${Pay}day"; # now, $Text gets "It's Payday" $Text = "It's $Pay"."day"; # another way to do it $Text = "It's " . $Pay . "day"; # and another way (I told you Tim-Toady) # i.e TMTOWTDI -------------------------------------------------------------

The case-shifting string escapes can be used to alter the case of letters brought in wi variable interpolation.

-------------------------------------------------------------$bigfred = "\Ufred"; # $bigfred is "FRED" $fred = "fred"; $bigfred = "\U$fred"; # same thing $capfred = "\u$fred"; # $capfred is "Fred" $barney = "\LBARNEY"; # $barney is now "barney" $capbarney = "\u\LBARNEY"; # $capbarney is now "Barney" $bigbarney = "BARNEY"; $capbarney = "\u\L$bigbarney"; # same -------------------------------------------------------------A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Scalars Exemplified : Array

A list is an ordered collection of scalar data. An array is a variable that holds a list. Each element of the array is a separate scalar variable with an independent scalar value. These values are ordered; that is, they have a particular sequence from the lowest to the highest element.
-------------------------------------------------------------(1,2,3) # array of three values 1, 2, and 3 ($b+$c, 17, Text) # three values (1.2 .. 5.2) # same as (1.2, 2.2, 3.2, 4.2, 5.2) (2 .. 6,10,12) # same as (2,3,4,5,6,10,12) ($a .. $b) # range determined by current values of $a and $b (1.3 .. 6.1) # same as (1.3,2.3,3.3,4.3,5.3) @X = ("Text_1","Text_2","Text_3","Text_4"); @X = qw(Text_1 Text_2 Text_3 Text_4); @huh = 1; # 1 is promoted to the list (1) automatically @XYZ = qw(one two); @ABC = (4,5,@XYZ,6,7); # @ABC becomes (4,5,"one","two",6,7) --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
More on Array and List Operations & Functions

Scalars Exemplified : Array

-------------------------------------------------------------($a,$b,$c) = (1,2,3); # give 1 to $a, 2 to $b, 3 to $c ($a,$b) = ($b,$a); # swap $a and $b ($d,@List) = ($a,$b,$c); # give $a to $d, and ($b,$c) to @List ($e,@List) = @List; # remove first element of @List to $e # this makes @List = ($c) and $e = $b @List = (4,5,6); $a = @List; ($a) = @List; # initialize @List # $a gets 3, the current length of @List # $a gets the first element of @List

@List = (7,8,9); $b = $List[0]; # give 7 to $b (first element of @List) $List[0] = 5; # now @List = (5,8,9) ($List[0],$List[1]) = ($List[1],$List[0]); # swap the first two @List[1,2] = (9,10); # change the last two values to 9 and 10 @who = (qw(Text_1 Text_2 Text_3 Text_4 Text_5))[0,3]; --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Scalars Exemplified : Array

Still More on Array and List Operations & Functions

-------------------------------------------------------------@List = (7,8,9); $X = 2; $c = $List[$X-1]; # $c gets $List[1], or 8 ($c) = @List[$X-1]; # same thing using slice @XList = (2,1,0); @ListBack = @List[@XList]; @List = (1,2,3); $List[6] = "ho"; # @List is now (1,2,3,undef,undef,undef,"ho") @List = qw(Text_1 Text_2 Text_3 Text_4 ); print $List[-1]; # prints "Text_4" print $List[$#List]; # prints "Text_4" push(@List,$Value); # like @List = (@List,$Value) $Value = pop(@List); # removes the last element of @List unshift(@List,$X); # like @List = ($X,@List); unshift(@List,$X,$b,$c); # like @ = ($X, $Y, $Z, @List); $X = shift(@List); # like ($X,@List) = @List; --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Scalars Exemplified : Array

If only we were done with stuff on Array and List Operations & Functions sort() reverse() chomp() and chop() ( or chop() and chomp() ) splice(), split(), join(), unpack(), grep() ......
-------------------------------------------------------------@XYZ = ("a","bb","ccc",1,2,3); $strange = "Now for @XYZ[@XYZ[3 .. 5]] here!"; # Really fishy!!!! @X = <STDIN>; # read standard input in a list context ------------------------------------------------------------

About undef

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Hashes are ................

Scalars Exemplified : Hashe

Hashes are like arrays, in that they are a collection of scalar data, with individual elements selected by some index value. Unlike a list array, the index values of a hash are not small nonnegative integers, but instead are arbitrary scalars. These scalars (called keys) are used later to retrieve the values from the array.

-------------------------------------------------------------$XYZ{"aaa"} = "bbb"; # creates key "aaa", value "bbb" $XYZ{234.5} = 456.7; # creates key "234.5", value 456.7 %XYZ = qw(1 a 2 b 3 c 4 d); --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Hash Functions
@List = values (%Hash); ($Key,$Value) = each(%Hash) delete $Hash{$Key}; # returns deleted value exists $Hash{$Key}; # boolean defined

Scalars Exemplified : Hashe

-------------------------------------------------------------@List = keys(%Hash);

%Hash_1{keys %Hash_2} = values %Hash_2; %Hash_1 = (%Hash_1, %Hash_2); # merge %Hash_2 into %Hash_1 %ABC = reverse %XYZ; --------------------------------------------------------------

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Control Structures : For control after all, we all need it Decisions (, the easy way)

Control Structures

-------------------------------------------------------------if () { do stuff } else { do some other stuff } if () { ... } elsif() { ... } elsif() { ... } ........ else{ ... } # multiple decisions may get confusing unless () { do stuff } else { do some other stuff } # Test for fallacy -------------------------------------------------------------

-------------------------------------------------------------while () { do stuff } until () { do stuff } do { do stuff } while (); do { do stuff } until (); for (initialization_expression;test_condition;increment_expression) { ... } # the quintessential for as for forever foreach $i (@List) { do stuff on $i obviously } --------------------------------------------------------------

Iterations (, can you have a programming language without them)

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Sbotns uruie

Defining your own functions (or lets say subroutines or just sub)

-------------------------------------------------------------sub subname { your normal programming stuff } --------------------------------------------------------------------------------------------------------------------------sub bigger_than { my($n,@values); # create some local variables ($n,@values) = @_; # split args into limit and values my(@result); # temporary for holding the return value foreach $_ (@values) # step through the arg list { if ($_ > $n) { push(@result,$_); } } # add it if eligible return @result; # return the final list } -------------------------------------------------------------

Of @_ & my (Its an operator, people)

local()

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Some Miscellaneous Stu

Formatted Output using printf()

-------------------------------------------------------------printf ("%15s %5d %10.2f\n", $s, $n, $r); -------------------------------------------------------------

-------------------------------------------------------------while ($Line=<STDIN>) { do stuff on $Line } while (<STDIN>) { do stuff on $_ } -------------------------------------------------------------

Of $_

-------------------------------------------------------------while (<>) { print $_; } -------------------------------------------------------------

Of Command Line Arguments

The use strict pragma and my();

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
Next , last & redo for loops Labeled Loops and that if this

Some More Miscellaneou

-------------------------------------------------------------LINE: while (<STDIN>) { last LINE if /^From: /; } exp2 unless exp1; # like: unless (exp1) { exp2; } exp2 while exp1; # like: while (exp1) { exp2; } -------------------------------------------------------------

-------------------------------------------------------------while (<>) { print $_; } -------------------------------------------------------------

Of Command Line Arguments

-------------------------------------------------------------chomp($n = <STDIN>); $i = 1; $i *= 2 until $i > $n; --------------------------------------------------------------

Why Perl rocks : Of this && that and many more stuff

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n
References :

Rfrne eeecs

Learning Perl, By Randal Schwartz, Tom Christiansen & Larry Wall, O' Reilly Publishers; ISBN 1-56592-284-0, 302 pages. Second Edition, July 1997 Programming Perl, By Larry Wall, Tom Christiansen & Randal L. Schwartz, O' Reilly Publishers; ISBN 1-56592-149-6, 670 pages. Second Edition, September 1996.

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Thank You

Open Minds : Open Source


The future is open.

Presentation Designed Using:

Version 1.1.3

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

P E R LA n: I n tr o d u c tio n

Questions & Doubts

Open Minds : Open Source


The future is open.

Presentation Designed Using:

Version 1.1.3

A l l n a m e s a n d l o g o s a r e r e g i s t e r e d t r a d Ankit Malhotra <Malhotra.Ankit@GMail.com> emarks of respective companies/foundations/persons.

Vous aimerez peut-être aussi