Difference between revisions of "PERL"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Basic Level)
 
(26 intermediate revisions by 4 users not shown)
Line 8: Line 8:
 
Useful for applications requiring Pattern Matching extensively
 
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.
 +
 
__TOC__
 
__TOC__
  
 
==Basic Level==
 
==Basic Level==
: '''Installation of Perl'''
+
: '''Topics'''
# Installation of Perl 5.14.2 on Ubuntu Linux
+
# '''Overview and Installation of Perl'''
#*Installing XAMPP in Linux
+
#*Installation of Perl 5.14.2 on Ubuntu Linux
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for Linux)
+
#**Installing XAMPP in Linux
#*Default Webserver directory will be set to "opt"
+
#*: (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
 
#:OR
 
#*Using default Perl installation available in Synaptic Package Manager
 
#*Using default Perl installation available in Synaptic Package Manager
# Installation of Perl 5.14.2 on Windows
+
#* Installation of Perl 5.14.2 on Windows
#*Installing XAMPP in Windows
+
#**Installing XAMPP in Windows
#: (XAMPP is a cumulative package consisting of Apache, PERL, PHP and MySQL Packages is available for 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"
+
#**Default Webserver directory will be set to "htdocs"
#:<br>
+
#'''Variables in Perl'''
#: '''Topics'''
+
#Variables in Perl
+
 
#*Variables are used for storing values, like text strings, numbers or arrays
 
#*Variables are used for storing values, like text strings, numbers or arrays
 
#*All variables in PERL start with a $ sign symbol
 
#*All variables in PERL start with a $ sign symbol
Line 31: Line 35:
 
#**$count = 1;
 
#**$count = 1;
 
#**$stringVar = ‘My Name is PERL’;
 
#**$stringVar = ‘My Name is PERL’;
#Comments in Perl
+
#'''Comments in Perl'''
 
#*Two types of comments -
 
#*Two types of comments -
 
#**Single Line
 
#**Single Line
Line 39: Line 43:
 
#** =cut =head or =begin =end
 
#** =cut =head or =begin =end
 
#**Start with = sign
 
#**Start with = sign
#:<br>
+
# '''for-foreach-Loop'''
#: '''Loops in Perl'''
+
# for-foreach-Loop
+
 
#* for Loop
 
#* for Loop
 
#** for loop is used to execute a piece of code for certain number of times
 
#** for loop is used to execute a piece of code for certain number of times
#** Syntax:<br><nowiki>for (initialization;condition;increment)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed multiple times</nowiki><br><nowiki>}</nowiki>
 
#** eg:<br><nowiki>for ($i=0; $i<=4; $i++)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>}</nowiki>
 
 
#* for-each Loop
 
#* for-each Loop
 
#** for-each loop is used to iterate a condition over an array
 
#** for-each loop is used to iterate a condition over an array
#** Syntax:<br><nowiki>foreach $variable (@array)</nowiki><br><nowiki>{</nowiki><br><nowiki>Perform action on each element of
+
# '''while-do-while Loops'''
array</nowiki><br><nowiki>}</nowiki>
+
#** eg:<br><nowiki>@myarray = (10, 20, 30);</nowiki><br><nowiki>foreach $var (@myarray)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Element of array is: $var \n”;</nowiki><br><nowiki>}</nowiki>
+
# while-do-while Loops
+
 
#* while Loop
 
#* while Loop
 
#** while loop executes a block of code while a condition is true.
 
#** while loop executes a block of code while a condition is true.
#** Syntax:<br><nowiki>while (condition)</nowiki><br><nowiki>{</nowiki><br><nowiki>Piece of code to be executed only while the condition is true</nowiki><br><nowiki>}</nowiki>
 
#** eg:<br><nowiki>$i = 0;</nowiki><br><nowiki>while ($i<=4)</nowiki><br><nowiki>{</nowiki><br><nowiki>print “Value of i: $i\n”;</nowiki><br><nowiki>$i++;</nowiki><br><nowiki>}</nowiki>
 
 
#* do-while Loop
 
#* do-while Loop
 
#** do-while loop will always execute the piece of code at-least once
 
#** 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
 
#** It will then check the condition and repeat the loop while the condition is true
#** 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>
+
# '''Conditional Statements'''
#** 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
+
 
#* if Statement
 
#* if Statement
 +
#** if statement is used to execute piece of code only if a specified condition is satisfied.
 
#* if-else Statement
 
#* if-else Statement
#* if-elsif-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.
 +
# '''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 Statement
#Data Structures in Perl
+
#** switch is conditional case statement. Satisfied case gets execute else the default case gets execute.
 +
#'''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
#* Associative Array or Hash
+
#** Array in PERL is ordered collection of data.
#More on Arrays
+
#** 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:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki>
 +
# '''Arrays'''
 +
#* Getting Last index of array
 
#* Getting length of an 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
 
#* Accessing element of an array
 
#* 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
+
#*** Using for-each loop
#** unshift
+
#''' Array functions'''
#** shift
+
#* push
#** split
+
#** Add element at the end of an array
#** qw
+
#* pop
#** sort
+
#** Remove element from the end of an array
#More on Hash
+
#* 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.
 +
# '''Hash in Perl'''
 
#* Accessing element of a hash
 
#* Accessing element of a hash
 
#* 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
#Functions in Perl
+
#'''Functions in Perl'''
 
#* Simple function
 
#* Simple function
 
#* Function with parameters
 
#* Function with parameters
 
#* Function which return single value
 
#* Function which return single value
 
#* Function which returns multiple values
 
#* Function which returns multiple values
#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.
 
#* End
 
#* 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==   
 
==Intermediate level==   
 
: '''Topics'''
 
: '''Topics'''
#Access Modifiers in PERL
+
#'''Access Modifiers in PERL'''
#* my
+
#* private variable - my
#* local
+
#** scope is in the block inside where it is declared.
#* our
+
#* lexically scoped variables - local
#Referencing & Dereferencing in Perl
+
#** 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.
 +
#'''Referencing & Dereferencing in Perl'''
 
#* Referencing
 
#* 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
 
#* De-referencing
#Special Variables in PERL
+
#** Get the actual entity being referred by reference.
#Sample Perl Programs
+
#**Demo of various examples
#Exception and error handling in PERL
+
#'''Special Variables in PERL'''
#* eval block in perl
+
#* Special variables have a predefined and special meaning in Perl.
#File Handling
+
#* These variables are denoted by usual variable indicator such as $, @, % along with punctuation characters.
#* open a file  
+
#'''File Handling'''
#* open a file in read mode
+
#* Open a file  
#* open a file in read, write mode
+
#* Open a File in Read Mode
#* open a file in append mode
+
#* Open a File in Write Mode
#* write into a file
+
#* Open a File in Append Mode
#* close the file handle
+
#* Close the FileHandle
#Including files and/or modules in a PERL program
+
#'''Exception and error handling in PERL'''
#* do
+
#* When an error occurs, exception and error handling helps to recover the program.
#* require
+
#*Methods used in Perl:
#* use
+
#** warn()
#Perl Modules
+
#** die()
#PERL Module library
+
#** eval()
#* CPAN
+
#'''Including files and/or modules in a PERL program'''
#Adding path to a default list of paths for a module
+
#* We can include the Perl modules or files by using the following methods.
#* @INC
+
#**  do: It includes the source code from other files into the current script file.
#Downloading required module onto windows or linux
+
#** use: It includes Perl module files only. Files get included before the actual execution of the code.
#* Downloading CPAN module
+
#** require: It includes both Perl programs and modules.
#**WindowsLinux
+
#'''Sample Perl Programs'''
# PERL & HTML
+
#*Includes all major topics that we covered so far in this sample program.
#* CGI module
+
#*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.
==Advanced level==  
+
#* weather_report.pl is the Perl program which makes use of this module file to give the required output
: '''Topics'''
+
#'''PERL Module library(CPAN)'''
#Function Prototyping
+
#* Comprehensive Perl Archive Network (CPAN) is the library of modules.
#Date & Time
+
#* User can make use of the existing modules available in CPAN
#* Current Time Stamp
+
#* New modules created by the user can be uploaded to CPAN so that other Perl users can make use of it.
#* Various Date formats
+
#'''Downloading CPAN module'''
#* Various operations that can be performed on date
+
#* Linux OS:
#Oops in Perl
+
#**There are several ways to download.
#* Object creation
+
#**Type cpan and press Enter.
#* Constructor
+
#**This gives us cpan prompt.
#* Destructor
+
#**Type install module name.
#* Accessing methods using an object
+
#* Windows OS:
#* Inheritance in Per
+
#**With installation of Perl on windows, a utility called PPM(Perl Package Module) gets installed.
#Exporting functions in Perl
+
#**Type ppm install module name.
#* EXPORT
+
#'''PERL & HTML'''
#* EXPORT_OK
+
#* To create HTML pages, Perl provides CGI module which creates CGI script with require HTML tags.
#Pattern Matching / Regular expression in Perl
+
#* 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.
#* Basics of pattern matching
+
#* Syntax
+
#* Modifiers
+
#Database handling
+
#* DBI module
+
#Multithreading
+
#* threads module
+
#Socket Programming
+
#* use IO::Socket::INET
+
#General information
+
#* Getting Perl version
+
#* Perl installation path
+
#* Information about the
+
#** module
+
#** it’s location
+
#** from where it is included in your perl script
+

Latest revision as of 17:05, 14 October 2020

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