PERL an Introduction

download PERL an Introduction

of 24

Transcript of PERL an Introduction

  • 8/3/2019 PERL an Introduction

    1/24

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    P E R L :A n I n t r o d u c t io n

    Welcome

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    2/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Questions & Doubts atthe end please..........

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    3/24

    P E R LA n

    I n t r o d u c t i o

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    * A Crash Course forthe uninitiated.

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    4/24

    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 moreadditions and .... Perl is released to the Usenet community where people give more feedbackand 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). Perlgrew (and still is growing at version 6) exponentially like the *x OS in features and portabilityto get .........

    Perl is designed to assist the programmer with common tasks that are probably too heavy or tooportability-sensitive for the shell, and yet too weird or short-lived or complicated to code in Cor 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. )

    P E R L :A n I n t r o d u c t io nHistory & Purpose

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    5/24

    perl

    #!/usr/bin/env perl

    Hello World Example :--------------------------------------------------------------#!/usr/bin/env perlprint ("Hello, world!\n");--------------------------------------------------------------

    Little Better : A program to greet a User Entered name.--------------------------------------------------------------#!/usr/bin/env perlprint ("Enter your name : ");$Name = chomp ($Name);print ("Hello $name. Welcome to Perl. ");--------------------------------------------------------------

    P E R L :A n I n t r o d u c t io nBasic Execution Ste

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    6/24

    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 inthe program is treated as the equivalent floating-point value.

    Float Literals--------------------------------------------------------------

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

    -12e-24 # negative 12 times 10 to the -24th (a very small negative number)

    --------------------------------------------------------------

    Integer Literals--------------------------------------------------------------

    12-2004

    0377 # 377 octal, same as 255 decimal

    -0xff # negative FF hex, same as -255 decimal

    --------------------------------------------------------------

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    7/24

    Of Strings :

    Single Quote (') strings: No Interpolation--------------------------------------------------------------'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--------------------------------------------------------------

    Double Quotes () strings: Variable & \ 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--------------------------------------------------------------

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

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    8/24

    Numerical Operators :

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

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    9/24

    Operator Precedence

    Literal Conversion : If a string value is used as an operand for a numeric operator (say, +), Perl automaticallyconverts 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 quietlyignored, so " 123.45fred" (with a leading space) converts to 123.45 with nary a warning.[8] Atthe 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 tointerpret hex and octal values.

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    10/24

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

    Of chop() and chomp()

    chop() returns the discarded character after removing it from the passed argumentchomp() returns the number of characters discarded after removing only the trailing newlinecharacters (, or whatever the input record separator $/ is set to.)

    Interpolation of Scalars into Strings--------------------------------------------------------------$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--------------------------------------------------------------

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    11/24

    P E R L :A n I n t r o d u c t io nA n I n t r o d u c t i o n t o s c a

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

    --------------------------------------------------------------

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    12/24

    P E R L :A n I n t r o d u c t io n

    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. Thesevalues are ordered; that is, they have a particular sequence from the lowest to the highestelement.--------------------------------------------------------------(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)--------------------------------------------------------------

    S c a l a r s E x e m p l i f i e d : A r r a

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    13/24

    P E R L :A n I n t r o d u c t io nS c a l a r s E x e m p l i f i e d : A r r a

    More on Array and List Operations & Functions--------------------------------------------------------------($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); # initialize @List

    $a = @List; # $a gets 3, the current length of @List($a) = @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];--------------------------------------------------------------

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    14/24

    P E R L :A n I n t r o d u c t io nS c a l a r s E x e m p l i f i e d : A r r a

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    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 @Listunshift(@List,$X); # like @List = ($X,@List);unshift(@List,$X,$b,$c); # like @ = ($X, $Y, $Z, @List);$X = shift(@List); # like ($X,@List) = @List;--------------------------------------------------------------

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    15/24

    P E R L :A n I n t r o d u c t io nS c a l a r s E x e m p l i f i e d : A r r a

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    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 = ; # read standard input in a list context-------------------------------------------------------------

    About undef

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    16/24

    P E R L :A n I n t r o d u c t io nScalars Exemplified : Hash

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    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 smallnonnegative integers, but instead are arbitrary scalars. These scalars (called keys) are usedlater to retrieve the values from the array.

    Hashes are ................

    --------------------------------------------------------------

    $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);

    --------------------------------------------------------------

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    17/24

    P E R L :A n I n t r o d u c t io nScalars Exemplified : Hash

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

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

    @List = values (%Hash);

    ($Key,$Value) = each(%Hash)

    delete $Hash{$Key}; # returns deleted value

    exists $Hash{$Key}; # boolean

    defined

    %Hash_1{keys %Hash_2} = values %Hash_2;%Hash_1 = (%Hash_1, %Hash_2); # merge %Hash_2 into %Hash_1

    %ABC = reverse %XYZ;

    --------------------------------------------------------------

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    18/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Control Structures : For control after all, we all need it

    Decisions (, the easy way)--------------------------------------------------------------if () { do stuff} else { do some other stuff}if () { ... } elsif() { ... } elsif() { ... } ........ else{ ... }

    # multiple decisions may get confusingunless () { do stuff} else { do some other stuff} # Test for fallacy-------------------------------------------------------------- Iterations (, can you have a programming language without them)--------------------------------------------------------------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 foreverforeach $i (@List) { do stuff on $i obviously}--------------------------------------------------------------

    Control Structures

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    19/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Defining your own functions (or lets say subroutines or just sub)--------------------------------------------------------------sub subname { your normal programming stuff }-------------------------------------------------------------- Of @_ & my (Its an operator, people)--------------------------------------------------------------sub bigger_than{my($n,@values); # create some local variables

    ($n,@values) = @_; # split args into limit and valuesmy(@result); # temporary for holding the return valueforeach $_ (@values) # step through the arg list{ if ($_ > $n) { push(@result,$_); } } # add it if eligiblereturn @result; # return the final list

    }--------------------------------------------------------------

    local()

    Subroutines

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    20/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

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

    Of $_--------------------------------------------------------------while ($Line=) { do stuff on $Line }

    while () { do stuff on $_ }--------------------------------------------------------------

    Of Command Line Arguments--------------------------------------------------------------while () { print $_; }--------------------------------------------------------------

    The use strict pragma and my();

    Some Miscellaneous Stu

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    21/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Next , last & redo forloops

    Labeled Loops and that if this--------------------------------------------------------------LINE: while () { last LINE if /^From: /; }

    exp2 unless exp1; # like: unless (exp1) { exp2; }exp2 while exp1; # like: while (exp1) { exp2; }-------------------------------------------------------------- Of Command Line Arguments--------------------------------------------------------------while () { print $_; }--------------------------------------------------------------

    Why Perl rocks : Of this && that and many more stuff--------------------------------------------------------------chomp($n = );

    $i = 1;$i *= 2 until $i > $n;--------------------------------------------------------------

    S o m e M o r e M i s c e l l a n e o u

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    22/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    References :

    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' ReillyPublishers; ISBN 1-56592-149-6, 670 pages. Second Edition, September 1996.

    References

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    23/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Thank

    You

    Open Minds :

    Open Source

    The future is open.

    Presentation Designed Using:

    Version 1.1.3

    P E R L

    mailto:[email protected]:[email protected]
  • 8/3/2019 PERL an Introduction

    24/24

    P E R L :A n I n t r o d u c t io n

    All names and logos are registered trademarks ofrespective companies/foundations/persons.

    Ankit Malhotra

    Questions

    & Doubts

    Open Minds :

    Open Source

    The future is open.

    Presentation Designed Using:

    Version 1.1.3

    mailto:[email protected]:[email protected]