perl language

Upload: selje

Post on 06-Apr-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 PERL Language

    1/29

  • 8/3/2019 PERL Language

    2/29

    Perly is a high-level, general-

    purpose, interpreted, dynamic programminglanguage.

    y was originally developed by Larry Wall in 1987as a general-purpose Unix scripting language to

    make report processing easier.y borrows features from other programminglanguages including C, shellscripting (sh), AWK, and sed.

  • 8/3/2019 PERL Language

    3/29

    y T he language provides powerful text processingfacilities without the arbitrary data length limits of many contemporary Unix tools, facilitating easy manipulation of text files.

    y Perl gained widespread popularity in the late 1990sas aCGI scripting language, in part due to its

    parsing abilities, but is now in sharp decline forthis application, where it is increasingly beingreplaced by more user-friendly CGI languages suchas PHP, Ruby and Python

  • 8/3/2019 PERL Language

    4/29

    y In addition to CGI, Perl is used for graphicsprogramming, systemadministration, network programming,finance, bioinformatics, and otherapplications. Perl is nicknamed "the Swiss Army chainsaw of programming languages"due to its flexibility and power. It is alsoreferred to as the "duct tape that holds theInternet together", in reference to itsubiquity and perceived inelegance.

  • 8/3/2019 PERL Language

    5/29

    Histo ryy Larry Wall began work on Perl in 1987, while

    working as a programmer at Unisys, and released

    version 1.0 to the comp.sources.misc newsgroup onDecember 18, 1987.T he language expanded rapidly over the next few years.

    y Perl 2, released in 1988, featured a better regularexpression engine. Perl 3, released in 1989, addedsupport for binary data streams.

  • 8/3/2019 PERL Language

    6/29

    y O riginally the only documentation for Perl wasa single (increasingly lengthy) man page. In1991,Programming Perl , known to many Perl

    programmers as the "Camel Book" because of its cover, was published and became the de facto reference for the language. At the sametime, the Perl version number was bumped to4, not to mark a major change in the languagebut to identify the version that wasdocumented by the book.

  • 8/3/2019 PERL Language

    7/29

    y Perl was originally named "Pearl", after the Parableof the Pearl from the Gospel of Matthew. Larry Wall wanted to give the language a short name with positive connotations; he claims that heconsidered (and rejected) every three- and four-letter word in the dictionary. He also considerednaming it after his wife Gloria. Wall discovered theexisting PEARL programming language beforePerl's official release and changed the spelling of the name.

  • 8/3/2019 PERL Language

    8/29

    y When referring to the language, the name isnormally capitalized ( Perl ) as a proper noun. When referring to the interpreter program itself,the name is often uncapitalized ( perl ) becausemost Unix-like file systems are case-sensitive.Before the release of the first editionof Programming Perl , it was common to refer tothe language as perl ; Randal L. Schwartz, however,

    capitalized the language's name in the book tomake it stand out better when typeset. T his casedistinction was subsequently documented ascanonical

  • 8/3/2019 PERL Language

    9/29

    y T here is some contention about the all-capsspelling "PERL", which the documentationdeclares incorrect and which some corecommunity members consider a sign of outsiders. T he name isoccasionally backronymed as Practical Extractionand Report Language , which appears at the top of the documentation and in some printedliterature. Several backronyms have beensuggested as equally canonical, including Wall'sown humorous Pathologically Eclectic RubbishLister . Indeed, Wall claims that the name wasintended to inspire many different expansions.

  • 8/3/2019 PERL Language

    10/29

    y Perl Variables:

    y Perl has 3 types of variables:

    y scalars;y arrays;y hashes;

  • 8/3/2019 PERL Language

    11/29

    y 1. Scalars:

    y Example on how to define a scalar variable in Perl: $var1 = "value" # a scalar variable var1 is defined and

    a string

    # "value" is assigned to that variable; $var2 = 100 # a scalar variable var2 is defined, and

    an#integer value is assigned.

    y Example: To print a scalar value we will use: print "$var1";

  • 8/3/2019 PERL Language

    12/29

    y 2. A rrays:

    y Example on how to define an array in Perl:

    @array1 = ( "Value1", "Value2", Value3");

    y Example on how to print an array:print " O ur array variable contains: @array1\n";

    y T his example displays all values from array1 array

  • 8/3/2019 PERL Language

    13/29

    y O ne element of an array:print "First element of the array is: $array1[0]";

    y

    As you might notice we ve defined array with @but printed a single value of that array using $.

    y It is also possible to print multiple values from anarray:

    print " O ur array contains: @array1[0..2]";

  • 8/3/2019 PERL Language

    14/29

    y It is also possible to print multiple values from an

    array:print " O ur array contains: @array1[0..2]";y T his example will print elements from 0 to element

    nr.2 from array1.y You can also print multiple distinct elements from

    array:print " O ur array contains: @array1[0,4,7]";

    y T his example will print only values for element 0, 4and 7.

    y N ote that in perl first value of an array is number 0.

  • 8/3/2019 PERL Language

    15/29

    y How to find the number of elements of an array?

    print "Number of elements of an array: $#array1";

    y Note that $#array1 in our example is number of elements, but because elements from an array inperl starts with value 0, the real number of elements of

    an array is $#array + 1.

  • 8/3/2019 PERL Language

    16/29

    y Perl functions for working with arrays:

    y pop - remove last element of an array:y push - add an element to the end of array;y shift - removes rst element of an array;y unshift - add an element to the beginning

    of array;y sort - sort an array.

  • 8/3/2019 PERL Language

    17/29

    y Pop Function (remove last element of an array):

    #!/usr/bin/ perl -w

    @array1 = ("Data1", "Data2", "Data3");print "Array1 values: @array1[0..$#array1]\n"; po p @array1;

    print "Array1 after applying pop function:@array1[0..$#array1]\n";

  • 8/3/2019 PERL Language

    18/29

    y Push Function (add an element to the end of array):

    #!/usr/bin/ perl -w@array1 = ("Data1", "Data2", "Data3");print "Array1 values: @array1[0..$#array1]\n"; push @array1, "Data4";

    print "Array1 after applying push function:@array1[0..$#array1]\n";

  • 8/3/2019 PERL Language

    19/29

    y Shift Function (removes rst element of an array):

    #!/usr/bin/ perl -w

    @array1 = ("Data1", "Data2", "Data3");print "Array1 values: @array1[0..$#array1]\n";shift @array1;

    print "Array1 after applying shift function:@array1[0..$#array1]\n";

  • 8/3/2019 PERL Language

    20/29

    y 3. Hashes

    - are types of variables defined as key - valuepair.

    y Example of defining a hash variable:%name_email = ("John", "john @exam ple.com" ,"George", "george @exam ple.com");

    y Another way to define a hash variable:

    %name_email = ( John => "john @exam ple.com",George => "george @exam ple.com", );

  • 8/3/2019 PERL Language

    21/29

    y Example of using hash variables:#!/usr/bin/ perl -w%name_email = ( "John", "john @exam ple.com","George", "george @exam ple.com"); print $name_email{"John"};

    y Note: W e ve used escape character to preserver @.

    y Also note that printing a hash variable means to printa scalar with value key between braces { }.

  • 8/3/2019 PERL Language

    22/29

    Perl c ont ro l st ruc t ure sy 1.Conditionals:y For testing conditionals within Perl if it is used:

    #!/usr/bin/ perl -w $var1 = 100; $var2 = 200;if ($var1 < $var2) { print "$var1 < $var2\n"; }

  • 8/3/2019 PERL Language

    23/29

    y Note: When evaluating expressions if variables arenumbers we will use mathematical operators ( < >= ===).

    y When we use string variables we use stringevaluation operators like gt (greater then) eq(equal) and so on.

    y

    Note: When we evaluate two numbers to beidentical, we use == operator (not = which is usedfor assigning values.

  • 8/3/2019 PERL Language

    24/29

    y Another example follows:#!/usr/bin/ perl -w

    $var1 = 400; $var2 = 200;if ($var1 < $var2) { print "$var1 < $var2\n";

    }elsif ($var1 > $var2) { print "$var1 > $var2\n"; }elsif function as a nested if.T he inverse test of if is unless function:unless ($var1 == $var2) { print "$var1"; }

  • 8/3/2019 PERL Language

    25/29

    y 2.Loopsy 2.1.For Loops3 ways to construct a loop using for .

    Example 1: For loop using C style:#!/usr/bin/ perl -w# for loo p exam ple 1 for ($i = 1; $i < 100; $i++) { print "$i\n"; }

  • 8/3/2019 PERL Language

    26/29

    y Example 2: for loops using ranges:

    #!/usr/bin/ perl -w# for loo p exam ple 2 $var1 = 1;

    $var2 = 100; $i = 1; for ($var1..$var2) { print "$i\n";

    $i+=1; $i = $i + 1; $i++; }

  • 8/3/2019 PERL Language

    27/29

    y

    Example 3: loop using foreach:

    #!/usr/bin/ perl -w# for loo p exam ple 3@array1 = ( "Val1", "Val2", "Val3", "Val4", "Val5"); foreach ( @array1) { print "$_\n";

    }

    y Note: $_ will print the current value of an array.

  • 8/3/2019 PERL Language

    28/29

  • 8/3/2019 PERL Language

    29/29

    y 2.3. Until Loopsy Until is negation of while. Here is an example:

    #!/usr/bin/ perl -w $var1 = 1; $var2 = 8;until ($var2 < $var1) { print "$var2\n"; $var2 -= 1; }