Difference between revisions of "PERL"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Basic Level)
Line 61: Line 61:
 
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki>
 
#** Syntax:<br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed while the condition is true</nowiki><br><nowiki>}do (while);</nowiki>
 
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki>
 
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>do</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}while ($i<=4);</nowiki>
#Conditional Statements
+
#:<br>
 +
#: '''Conditional Statements'''
 +
# if-if-else Statements
 
#* if Statement
 
#* if Statement
 +
#** if statement is used to execute piece of code only if a specified condition is satisfied.
 +
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki>
 +
#** eg:
 
#* if-else Statement
 
#* if-else Statement
 +
#** if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.
 +
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>else { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki>
 +
#** eg:
 +
# if-elsif-else-switch Statements
 
#* if-elsif-else Statement
 
#* if-elsif-else Statement
 +
#** if-elsif-else statement is used to select one of several blocks of code to be executed.
 +
#** Syntax:<br><nowiki>if (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed;</nowiki><br><nowiki>}</nowiki><nowiki>elsif (other condition) { </nowiki><br><nowiki>Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki><nowiki>else{</nowiki><br><nowiki>#piece of code to be executed if both the above conditions are false.</nowiki><br><nowiki>}</nowiki>
 +
#** eg:
 
#* switch Statement
 
#* switch Statement
 +
#** switch statement is used to select one of many blocks of code to be executed.
 +
#** There were no Switch/Case in perl prior to 5.8 version.
 +
#** After 5.8 PERL provided Switch module.
 +
#** Syntax:<br><nowiki>use switch;</nowiki><br><nowiki>$variable;</nowiki><br><nowiki>switch ($variable){</nowiki><br><nowiki>case(1) { ....}</nowiki><br><nowiki>case(2) { ....}</nowiki><br><nowiki>case(3) { ....}</nowiki><br> <nowiki>else { ....}</nowiki> <br> <nowiki>}</nowiki>
 +
#** eg:<br><nowiki>use switch;</nowiki><br><nowiki>$var = 2;</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case(1) { $i = “One”}</nowiki><br><nowiki>case(2) { $i = “Two”}</nowiki><br> <nowiki>case(3) { $i = “Three”}</nowiki><br><nowiki>else { $i = “Other”}</nowiki><br><nowiki>}</nowiki><br><nowiki>$i will become 2</nowiki>
 
#Data Structures in Perl
 
#Data Structures in Perl
 
#* Scalar
 
#* Scalar
 +
#** These are the basic variables in PERL.
 +
#** It can hold any kind of type viz. string, number etc.
 +
#** eg: $variable = 9;<br>$variable = ‘This is string type of variable’;
 
#* Array
 
#* Array
 +
#** Array in PERL is ordered collection of data.
 +
#** It can hold data of any type.
 +
#** Array index starts from zero.
 +
#** eg: @array = (1, 5, 6, ‘abc’, 7);
 
#* Associative Array or Hash
 
#* Associative Array or Hash
 +
#** Associative array or Hash in PERL is un-ordered collection of data.
 +
#** It is a key value pair.
 +
#** Key cannot be duplicate in hash whereas value can be.
 +
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki>
 
#More on Arrays
 
#More on Arrays
 +
#* Getting Last index of array
 +
#** eg:<br><nowiki>@array =  (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>print “Last index of an array is: $#array”;</nowiki><br><nowiki># prints… Last index of an array is: 4</nowiki>
 
#* Getting length of an array
 
#* Getting length of an array
 +
#** To get the length, add 1 to last index of an array
 +
#** eg: <nowiki>print “Length of an array is: ”, $#array+1;</nowiki><br><nowiki># prints.. Length of an array is: 5</nowiki>
 +
#** Other way is use scalar function on array or assign array to a scalar variable.
 +
#** eg:<br><nowiki>scalar (@array);</nowiki><br><nowiki>$length = @array;</nowiki>
 
#* Accessing element of an array
 
#* Accessing element of an array
 +
#** eg:<br><nowiki>@array =  (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki># print the 4th element of an array</nowiki><nowiki>print $array[3];</nowiki>
 +
#** prints…. “abc”
 
#* Looping over an array
 
#* Looping over an array
#* Basic array functions
+
#** There are two ways to loop over an array
#** push
+
#*** Using for loop
#** pop
+
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>for ($i=0; $i<$#array; $i++) {</nowiki><br><nowiki>print $array[$i];</nowiki><br><nowiki>}</nowiki>
#** unshift
+
#*** Using for-each loop
#** shift
+
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki>
#** split
+
# Basic array functions
#** qw
+
#* push
#** sort
+
#** Add element at the end of an array
 +
#* pop
 +
#** Remove element from the end of an array
 +
#* unshift
 +
#** Add element at the start of an array
 +
#* shift
 +
#** Remove element of an array from the start.
 +
#* split
 +
#** This function splits the string and makes an array of it.
 +
#** eg:<br><nowiki>$var = ‘Hello World’</nowiki><br><nowiki>@array = split (/ /, $var);</nowiki><br><nowiki>$var will get split on space and @array will contain 2 elements Hello  World</nowiki>
 +
#* qw
 +
#** qw stands for “Quoted word”
 +
#** It returns a list of word separated by white spaces.
 +
#** eg:<br><nowiki>@array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’); </nowiki>
 +
#* sort
 +
#** sorts the array in alphabatical order.
 
#More on Hash
 
#More on Hash
 
#* Accessing element of a hash
 
#* Accessing element of a hash
 +
#** Syntax:<br><nowiki> %hash = (</nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki><br><nowiki>print $hash{Name};</nowiki><br><nowiki> #prints… John</nowiki>
 
#* Basic hash functions
 
#* Basic hash functions
 
#** keys
 
#** keys
 +
#*** Returns keys of a hash
 
#** values
 
#** values
 +
#*** Returns values of a hash
 
#** each
 
#** each
 +
#*** Retrieve the next key/value pair from a hash
 
#* Looping over a hash
 
#* Looping over a hash
 +
#** Syntax:<br><nowiki> foreach ($key = (keys %hash)) {</nowiki> <br><nowiki> print $hash{$key};</nowiki> <br><nowiki> }</nowiki> <br><nowiki>OR</nowiki> <br><nowiki>foreach (($key, $value) = each (%hash)) {{</nowiki> <br><nowiki>print “Key: $key & value: $value”;{</nowiki> <br><nowiki>}{</nowiki>
 
#Functions in Perl
 
#Functions in Perl
 
#* Simple function
 
#* Simple function
 +
#** Syntax:<br><nowiki>sub sample_func  {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>}</nowiki>
 
#* Function with parameters
 
#* Function with parameters
 +
#** Syntax:<br><nowiki>sub func_with_parameters {</nowiki><br><nowiki>($variable) = @_;</nowiki><br><nowiki># @_ contains the arguments passed to function.</nowiki><br><nowiki>#This is a special PERL variable.</nowiki><br><nowiki>}</nowiki>
 
#* Function which return single value
 
#* Function which return single value
 +
#** Syntax:<br><nowiki>sub return_single_value {</nowiki><br><nowiki>#piece of code</nowiki><br><br><br><nowiki>return $variable;</nowiki><br><nowiki>}</nowiki>
 
#* Function which returns multiple values
 
#* Function which returns multiple values
 +
#** Syntax:<br><nowiki>sub return_multiple_value {</nowiki><br><nowiki>#piece of code</nowiki><br><nowiki>return ($variable1, $variable2);</nowiki><br><nowiki>}</nowiki>
 
#Blocks in Perl
 
#Blocks in Perl
 
#* Begin
 
#* Begin
 +
#** This block executes at the compilation time once it is defined.
 +
#** Anything which needs to be included before execution of the rest of the code is written here.
 +
#** Syntax:<br><nowiki>begin {</nowiki><br><nowiki>#piece of code to be executed at the start</nowiki><br><nowiki>}</nowiki>
 
#* End
 
#* End
 +
#** This block executes at the end.
 +
#** Anything which needs to be executed at last is written here.
 +
#** Syntax:<br><nowiki>end {</nowiki><br><nowiki>#piece of code to be executed at the end</nowiki><br><nowiki>}</nowiki>
  
 
==Intermediate level==   
 
==Intermediate level==   

Revision as of 11:33, 12 February 2013

Introduction to Perl

Perl (Practical Extraction & Reporting Language) is widely used open-source language. It was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular amongst programmers. Larry Wall continues to work on development of the core language, and its upcoming version, Perl 6. Perl borrows features from other programming languages including C, shell scripting (sh), AWK, and sed. The language provides powerful text processing facilities, facilitating easy manipulation of text files. Perl gained widespread popularity in the late 1990s as a CGI scripting language, in part due to its parsing abilities.

Perl can be used as a very simple and easy to use programming language. This language helps programmer to write a simple piece of code against the heavy / complicated shell or C programming. This language is portable and reliable. Useful for applications requiring Pattern Matching extensively

Basic Level

Installation of Perl
  1. Installation of Perl 5.14.2 on Ubuntu Linux
    • Installing XAMPP in Linux
    (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)
    • Default Webserver directory will be set to "opt"
    OR
    • Using default Perl installation available in Synaptic Package Manager
  2. Installation of Perl 5.14.2 on Windows
    • Installing XAMPP in Windows
    (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Windows)
    • Default Webserver directory will be set to "htdocs"

    Topics
  3. Variables in Perl
    • Variables are used for storing values, like text strings, numbers or arrays
    • All variables in PERL start with a $ sign symbol
    • Declaring a variable in PERL: $var_name = value;
    • e.g:
      • $count = 1;
      • $stringVar = ‘My Name is PERL’;
  4. Comments in Perl
    • Two types of comments -
      • Single Line
      • Multi Line
    • Single Line comment starts with the symbol #
    • Multi Line comment used to comment a chunk of code
      • =cut =head or =begin =end
      • Start with = sign

    Loops in Perl
  5. for-foreach-Loop
    • for Loop
      • for loop is used to execute a piece of code for certain number of times
      • Syntax:
        for (initialization;condition;increment)
        {
        Piece of code to be executed multiple times
        }
      • eg:
        for ($i=0; $i<=4; $i++)
        {
        print “Value of i: $i\n”;
        }
    • for-each Loop
      • for-each loop is used to iterate a condition over an array
      • Syntax:
        foreach $variable (@array)
        {
        Perform action on each element of array
        }
      • eg:
        @myarray = (10, 20, 30);
        foreach $var (@myarray)
        {
        print “Element of array is: $var \n”;
        }
  6. while-do-while Loops
    • while Loop
      • while loop executes a block of code while a condition is true.
      • Syntax:
        while (condition)
        {
        Piece of code to be executed only while the condition is true
        }
      • eg:
        $i = 0;
        while ($i<=4)
        {
        print “Value of i: $i\n”;
        $i++;
        }
    • do-while Loop
      • do-while loop will always execute the piece of code at-least once
      • It will then check the condition and repeat the loop while the condition is true
      • Syntax:
        do
        {
        Piece of code to be executed while the condition is true
        }do (while);
      • eg:
        $i = 0;
        do
        {
        print “Value of i: $i\n”;
        $i++;
        }while ($i<=4);

    Conditional Statements
  7. if-if-else Statements
    • if Statement
      • if statement is used to execute piece of code only if a specified condition is satisfied.
      • Syntax:
        if (condition)
        {
        Piece of code to be executed;
        }
      • eg:
    • if-else Statement
      • if-else statement is used to execute piece of code if a condition is satisfied or another code if the condition is false.
      • Syntax:
        if (condition)
        {
        Piece of code to be executed;
        }else {
        Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed
        }
      • eg:
  8. if-elsif-else-switch Statements
    • if-elsif-else Statement
      • if-elsif-else statement is used to select one of several blocks of code to be executed.
      • Syntax:
        if (condition)
        {
        Piece of code to be executed;
        }elsif (other condition) {
        Another piece of code;<br> # if the above if condition evaluates to false, this another piece of code will be executed
        }else{
        #piece of code to be executed if both the above conditions are false.
        }
      • eg:
    • switch Statement
      • switch statement is used to select one of many blocks of code to be executed.
      • There were no Switch/Case in perl prior to 5.8 version.
      • After 5.8 PERL provided Switch module.
      • Syntax:
        use switch;
        $variable;
        switch ($variable){
        case(1) { ....}
        case(2) { ....}
        case(3) { ....}
        else { ....}
        }
      • eg:
        use switch;
        $var = 2;
        switch ($var){
        case(1) { $i = “One”}
        case(2) { $i = “Two”}
        case(3) { $i = “Three”}
        else { $i = “Other”}
        }
        $i will become 2
  9. Data Structures in Perl
    • Scalar
      • These are the basic variables in PERL.
      • It can hold any kind of type viz. string, number etc.
      • eg: $variable = 9;
        $variable = ‘This is string type of variable’;
    • Array
      • Array in PERL is ordered collection of data.
      • It can hold data of any type.
      • Array index starts from zero.
      • eg: @array = (1, 5, 6, ‘abc’, 7);
    • Associative Array or Hash
      • Associative array or Hash in PERL is un-ordered collection of data.
      • It is a key value pair.
      • Key cannot be duplicate in hash whereas value can be.
      • eg:
        %hash = (
        ‘Name’ => ‘John’,
        ‘Department’ => ‘Finance’
        );
  10. More on Arrays
    • Getting Last index of array
      • eg:
        @array = (1, 5, 6, ‘abc’, 7);
        print “Last index of an array is: $#array”;
        # prints… Last index of an array is: 4
    • Getting length of an array
      • To get the length, add 1 to last index of an array
      • eg: print “Length of an array is: ”, $#array+1;
        # prints.. Length of an array is: 5
      • Other way is use scalar function on array or assign array to a scalar variable.
      • eg:
        scalar (@array);
        $length = @array;
    • Accessing element of an array
      • eg:
        @array = (1, 5, 6, ‘abc’, 7);
        # print the 4th element of an arrayprint $array[3];
      • prints…. “abc”
    • Looping over an array
      • There are two ways to loop over an array
        • Using for loop
        • eg:
          @array = (1, 5, 6, ‘abc’, 7);
          for ($i=0; $i<$#array; $i++) {
          print $array[$i];
          }
        • Using for-each loop
        • eg:
          @array = (1, 5, 6, ‘abc’, 7);
          foreach $var (@array) {
          print $var;
          }
  11. Basic array functions
    • push
      • Add element at the end of an array
    • pop
      • Remove element from the end of an array
    • unshift
      • Add element at the start of an array
    • shift
      • Remove element of an array from the start.
    • split
      • This function splits the string and makes an array of it.
      • eg:
        $var = ‘Hello World’
        @array = split (/ /, $var);
        $var will get split on space and @array will contain 2 elements Hello World
    • qw
      • qw stands for “Quoted word”
      • It returns a list of word separated by white spaces.
      • eg:
        @array = qw (Hello world) this is equivalent to @array = (‘Hello’, ‘World’);
    • sort
      • sorts the array in alphabatical order.
  12. More on Hash
    • Accessing element of a hash
      • Syntax:
        %hash = (
        ‘Name’ => ‘John’,
        ‘Department’ => ‘Finance’
        );
        print $hash{Name};
        #prints… John
    • Basic hash functions
      • keys
        • Returns keys of a hash
      • values
        • Returns values of a hash
      • each
        • Retrieve the next key/value pair from a hash
    • Looping over a hash
      • Syntax:
        foreach ($key = (keys %hash)) {
        print $hash{$key};
        }
        OR
        foreach (($key, $value) = each (%hash)) {{
        print “Key: $key & value: $value”;{
        }{
  13. Functions in Perl
    • Simple function
      • Syntax:
        sub sample_func {
        #piece of code
        }
    • Function with parameters
      • Syntax:
        sub func_with_parameters {
        ($variable) = @_;
        # @_ contains the arguments passed to function.
        #This is a special PERL variable.
        }
    • Function which return single value
      • Syntax:
        sub return_single_value {
        #piece of code


        return $variable;
        }
    • Function which returns multiple values
      • Syntax:
        sub return_multiple_value {
        #piece of code
        return ($variable1, $variable2);
        }
  14. Blocks in Perl
    • Begin
      • This block executes at the compilation time once it is defined.
      • Anything which needs to be included before execution of the rest of the code is written here.
      • Syntax:
        begin {
        #piece of code to be executed at the start
        }
    • End
      • This block executes at the end.
      • Anything which needs to be executed at last is written here.
      • Syntax:
        end {
        #piece of code to be executed at the end
        }

Intermediate level

Topics
  1. Access Modifiers in PERL
    • my
    • local
    • our
  2. Referencing & Dereferencing in Perl
    • Referencing
    • De-referencing
  3. Special Variables in PERL
  4. Sample Perl Programs
  5. Exception and error handling in PERL
    • eval block in perl
  6. File Handling
    • open a file
    • open a file in read mode
    • open a file in read, write mode
    • open a file in append mode
    • write into a file
    • close the file handle
  7. Including files and/or modules in a PERL program
    • do
    • require
    • use
  8. Perl Modules
  9. PERL Module library
    • CPAN
  10. Adding path to a default list of paths for a module
    • @INC
  11. Downloading required module onto windows or linux
    • Downloading CPAN module
      • WindowsLinux
  12. PERL & HTML
    • CGI module

Advanced level

Topics
  1. Function Prototyping
  2. Date & Time
    • Current Time Stamp
    • Various Date formats
    • Various operations that can be performed on date
  3. Oops in Perl
    • Object creation
    • Constructor
    • Destructor
    • Accessing methods using an object
    • Inheritance in Per
  4. Exporting functions in Perl
    • EXPORT
    • EXPORT_OK
  5. Pattern Matching / Regular expression in Perl
    • Basics of pattern matching
    • Syntax
    • Modifiers
  6. Database handling
    • DBI module
  7. Multithreading
    • threads module
  8. Socket Programming
    • use IO::Socket::INET
  9. General information
    • Getting Perl version
    • Perl installation path
    • Information about the
      • module
      • it’s location
      • from where it is included in your perl script

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey, Nirmala Venkat, PoojaMoolya