PERL

From Script | Spoken-Tutorial
Jump to: navigation, search

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

The Spoken Tutorial Effort for Perl Basic level has been contributed by Amol Brahmankar from Pune and Intermediate level by Nirmala Venkat from Spoken Tutorials supported with domain reviews by Namrata Gaikwad from Pune.

Learners: UG/PG CSE/IT/CS students to learn programming of business applications.

Basic Level

Topics
  1. Overview and Installation of Perl
    • 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
    • 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"
  2. 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’;
  3. 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
  4. for-foreach-Loop
    • for Loop
      • for loop is used to execute a piece of code for certain number of times
    • for-each Loop
      • for-each loop is used to iterate a condition over an array
  5. while-do-while Loops
    • while Loop
      • while loop executes a block of code while a condition is true.
    • 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
  6. Conditional Statements
    • if Statement
      • if statement is used to execute piece of code only if a specified condition is satisfied.
    • 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.
  7. More Conditional Statements
    • if-elsif-else statement
      • if-elsif-else conditional statement is used to check specific condition and if it is true execute the respective block else execute the default else block.
    • switch Statement
      • switch is conditional case statement. Satisfied case gets execute else the default case gets execute.
  8. 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);
    • 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’
        );
  9. Arrays
    • Getting Last index of array
    • Getting length of an array
      • To get the length, add 1 to last index of an array
      • Other way is use scalar function on array or assign array to a scalar variable.
    • Accessing element of an array
    • Looping over an array
      • There are two ways to loop over an array
        • Using for loop
        • Using for-each loop
  10. 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.
    • qw
      • qw stands for “Quoted word”
      • It returns a list of word separated by white spaces.
    • sort
      • sorts the array in alphabetical order.
  11. Hash in Perl
    • Accessing element of a hash
    • 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
  12. Functions in Perl
    • Simple function
    • Function with parameters
    • Function which return single value
    • Function which returns multiple values
  13. 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.
    • End
      • This block executes at the end.
      • Anything which needs to be executed at last is written here.
    • UNITCHECK blocks
    • CHECK blocks
    • INIT blocks

Intermediate level

Topics
  1. Access Modifiers in PERL
    • private variable - my
      • scope is in the block inside where it is declared.
    • lexically scoped variables - local
      • that means they get the temporary value inside the block where it is used
    • global variables - our
      • can be accessed without giving package name while accessing it in another package.
  2. Referencing & Dereferencing in Perl
    • Referencing
      • Create a reference by adding \ (backward slash)
      • Demo of various examples
      • Add, remove, access elements of array reference / hash reference in the script with examples.
    • De-referencing
      • Get the actual entity being referred by reference.
      • Demo of various examples
  3. Special Variables in PERL
    • Special variables have a predefined and special meaning in Perl.
    • These variables are denoted by usual variable indicator such as $, @, % along with punctuation characters.
  4. File Handling
    • Open a file
    • Open a File in Read Mode
    • Open a File in Write Mode
    • Open a File in Append Mode
    • Close the FileHandle
  5. Exception and error handling in PERL
    • When an error occurs, exception and error handling helps to recover the program.
    • Methods used in Perl:
      • warn()
      • die()
      • eval()
  6. Including files and/or modules in a PERL program
    • We can include the Perl modules or files by using the following methods.
      • do: It includes the source code from other files into the current script file.
      • use: It includes Perl module files only. Files get included before the actual execution of the code.
      • require: It includes both Perl programs and modules.
  7. Sample Perl Programs
    • Includes all major topics that we covered so far in this sample program.
    • This program will give the output of various weather forecast reports of a region.
    • Weather.pm is a module that has a complex data structure to hold the data required for this program.
    • weather_report.pl is the Perl program which makes use of this module file to give the required output
  8. PERL Module library(CPAN)
    • Comprehensive Perl Archive Network (CPAN) is the library of modules.
    • User can make use of the existing modules available in CPAN
    • New modules created by the user can be uploaded to CPAN so that other Perl users can make use of it.
  9. Downloading CPAN module
    • Linux OS:
      • There are several ways to download.
      • Type cpan and press Enter.
      • This gives us cpan prompt.
      • Type install module name.
    • Windows OS:
      • With installation of Perl on windows, a utility called PPM(Perl Package Module) gets installed.
      • Type ppm install module name.
  10. PERL & HTML
    • To create HTML pages, Perl provides CGI module which creates CGI script with require HTML tags.
    • There are different methods which CGI modules provide to add header, adding fields to the page, retrieving the values of the parameters posted on to the form.

Contributors and Content Editors

AmolBrahmankar, Nancyvarkey, Nirmala Venkat, PoojaMoolya