Difference between revisions of "PERL"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Introduction to Perl)
 
(19 intermediate revisions by 4 users not shown)
Line 9: Line 9:
 
'''
 
'''
  
The Spoken Tutorial Effort for '''Perl''' has being contributed by '''Amol Brahmankar''' from Pune supported with domain reviews by '''Namrata Gaikwad''' from Pune.
+
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 34: 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 42: 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>
+
#:<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.
 
#** 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.
 
#** 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;if the above if condition evaluates to false, this another piece of code will be executed</nowiki><br><nowiki>}</nowiki>
+
# '''More Conditional Statements'''
#** eg:
+
#* if-elsif-else statement
# if-elsif-else-switch Statements
+
#** 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.
#* 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;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.
+
#** switch is conditional case statement. Satisfied case gets execute else the default case gets execute.
#** There were no Switch/Case in perl prior to 5.8 version.
+
#'''Data Structures in Perl'''
#** 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
+
 
#* Scalar
 
#* Scalar
 
#** These are the basic variables in PERL.  
 
#** These are the basic variables in PERL.  
Line 96: Line 74:
 
#** Array index starts from zero.
 
#** Array index starts from zero.
 
#** eg: @array = (1, 5, 6, ‘abc’, 7);
 
#** eg: @array = (1, 5, 6, ‘abc’, 7);
#* Associative Array or Hash
+
#* Hash
 
#** Associative array or Hash in PERL is un-ordered collection of data.
 
#** Associative array or Hash in PERL is un-ordered collection of data.
 
#** It is a key value pair.  
 
#** It is a key value pair.  
 
#** Key cannot be duplicate in hash whereas value can be.
 
#** 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>
 
#** eg:<br><nowiki>%hash = ( </nowiki><br><nowiki>‘Name’ => ‘John’,</nowiki><br><nowiki>‘Department’ => ‘Finance’</nowiki><br><nowiki>);</nowiki>
#More on Arrays
+
# '''Arrays'''
 
#* Getting Last index of array
 
#* 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
 
#** 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.
 
#** 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
 
#** There are two ways to loop over an array
 
#** There are two ways to loop over an array
 
#*** Using for loop
 
#*** Using for loop
#*** 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>
 
 
#*** Using for-each loop
 
#*** Using for-each loop
#*** eg:<br><nowiki>@array = (1, 5, 6, ‘abc’, 7);</nowiki><br><nowiki>foreach $var (@array) {</nowiki><br><nowiki>print $var;</nowiki><br><nowiki>}</nowiki>
+
#''' Array functions'''
# Basic array functions
+
 
#* push
 
#* push
 
#** Add element at the end of an array
 
#** Add element at the end of an array
Line 129: Line 100:
 
#* split
 
#* split
 
#** This function splits the string and makes an array of it.
 
#** 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
 
#** qw stands for “Quoted word”
 
#** qw stands for “Quoted word”
 
#** It returns a list of word separated by white spaces.
 
#** 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
 
#* sort
#** sorts the array in alphabatical order.
+
#** sorts the array in alphabetical order.
#More on Hash
+
# '''Hash in Perl'''
 
#* 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
Line 147: Line 115:
 
#*** Retrieve the next key/value pair from a hash
 
#*** 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.
 
#** 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.
 
#** 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.
 
#** This block executes at the end.
 
#** Anything which needs to be executed at last is written here.
 
#** 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>
+
#*UNITCHECK blocks
 +
#*CHECK blocks
 +
#*INIT blocks
  
 
==Intermediate level==   
 
==Intermediate level==   
 
: '''Topics'''
 
: '''Topics'''
#Access Modifiers in PERL
+
#'''Access Modifiers in PERL'''
#* my
+
#* private variable - my
#** this is kind of private variable.
+
 
#** scope is in the block inside where it is declared.
 
#** scope is in the block inside where it is declared.
#* local
+
#* lexically scoped variables - local
#** this kind of variables are lexically scoped variables  
+
 
#** that means they get the temporary value inside the block where it is used  
 
#** that means they get the temporary value inside the block where it is used  
#** as soon as block ends it gets the earlier value.
+
#* global variables - our
#** ie, 'local' temporarily changes the value of the variable, but only within the scope it exists.
+
#* our
+
#** this is kind of global variables in perl
+
 
#** can be accessed without giving package name while accessing it in another package.
 
#** can be accessed without giving package name while accessing it in another package.
#** e.g:<br><nowiki>package first;</nowiki><br><nowiki>our $varInFirst = 10;</nowiki><br><nowiki>package second;</nowiki><br><nowiki>print $varInFirst; # prints 10</nowiki>
+
#'''Referencing & Dereferencing in Perl'''
#Referencing & Dereferencing in Perl
+
 
#* Referencing
 
#* Referencing
#** In perl we can create a reference by adding \ (backward slash) to it.
+
#** Create a reference by adding \ (backward slash)
#** eg:<br><nowiki>$variable = 10;</nowiki><br><nowiki>$variableRef = \$variable;</nowiki><br><nowiki>@array = (1, 2 , 3);</nowiki><br><nowiki>$arrayRef = \@array;</nowiki><br><nowiki>%hash = (</nowiki><br><nowiki>‘Emp Id’ => 1000,</nowiki><br><nowiki>‘Name’  => ‘Peter’</nowiki><br><nowiki>);</nowiki><br><nowiki>$hashRef = \%hash;</nowiki>
+
#**Demo of various examples
#** Note: Reference in perl is nothing but a perl scalar variable holding address of the entity being referred.
+
#**Add, remove, access elements of array reference / hash reference in the script with examples.
 
#* De-referencing
 
#* De-referencing
#** Dereferencing is the mean by which we can get the actual entity being referred by reference.
+
#** Get the actual entity being referred by reference.
#** It is done by putting appropriate identifier in front of the reference variable.
+
#**Demo of various examples
#** eg:<br><nowiki>$scalar = $$scalarRef;</nowiki><br><nowiki>@array = @$arrayRef;</nowiki><br><nowiki>%hash = %$hashRef;</nowiki>
+
#'''Special Variables in PERL'''
#Special Variables in PERL
+
#* Special variables have a predefined and special meaning in Perl.
#* These variables in perl have some special meaning.
+
#* These variables are denoted by usual variable indicator such as $, @, % along with punctuation characters.  
#Sample Perl Programs
+
#'''File Handling'''
#Exception and error handling in PERL
+
#* Open a file  
#* eval block in perl
+
#* Open a File in Read Mode
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception
+
#* Open a File in Write Mode
#** or to know exactly where the code is failing with the type of error.
+
#* Open a File in Append Mode
#** eg:<br><nowiki>eval {</nowiki><br><nowiki>my $var = 10;</nowiki><br><nowiki>my $result = $var / 0;</nowiki><br><nowiki>};</nowiki><br><br><nowiki>if ($@) {</nowiki><br><nowiki>die “Error: $!”;</nowiki><br><nowiki># This will terminate the execution of the program.</nowiki><br><nowiki>If user wants to ignore this and just want to know the error use print instead of die.</nowiki><br><nowiki>}</nowiki>
+
#* Close the FileHandle
#File Handling
+
#'''Exception and error handling in PERL'''
#* To open a file  
+
#* When an error occurs, exception and error handling helps to recover the program.
#** open FH, FileName.txt;  # FH is the file handle
+
#*Methods used in Perl:
#* To open a file in read mode
+
#** warn()
#** open FH, "<FileName.txt";
+
#** die()
#* To open a file in read, write mode
+
#** eval()
#** open FH, ">FileName.txt";
+
#'''Including files and/or modules in a PERL program'''
#* To open a file in append mode
+
#* We can include the Perl modules or files by using the following methods.
#** open FH, ">>FileName.txt";
+
#** do: It includes the source code from other files into the current script file.
#* Looping on each line can be done as
+
#** use: It includes Perl module files only. Files get included before the actual execution of the code.
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki>
+
#**  require: It includes both Perl programs and modules.
#* write into a file
+
#'''Sample Perl Programs'''
#** print FH "I am writing in a file";
+
#*Includes all major topics that we covered so far in this sample program.
#* close the file handle
+
#*This program will give the output of various weather forecast reports of a region.
#** close FH
+
#* Weather.pm is a module that has a complex data structure to hold the data required for this program.
#Including files and/or modules in a PERL program
+
#* weather_report.pl is the Perl program which makes use of this module file to give the required output
#* do
+
#'''PERL Module library(CPAN)'''
#** it includes a file without checking that the file is already included or not
+
#* Comprehensive Perl Archive Network (CPAN) is the library of modules.
#* require
+
#* User can make use of the existing modules available in CPAN
#** it includes a file if it not included already.
+
#* New modules created by the user can be uploaded to CPAN so that other Perl users can make use of it.
#* use
+
#'''Downloading CPAN module'''
#** it includes perl module files only.  
+
#* Linux OS:
#** Files get included before the actual execution of the code gets start.
+
#**There are several ways to download.
#Perl Modules
+
#**Type cpan and press Enter.
#* These are the .pm files.  
+
#**This gives us cpan prompt.
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.
+
#**Type install module name.
#* e.g:When we write<br>use DBI;<br>This gives me access to the functions already written to connect to DB and for querying DB.
+
#* Windows OS:
#PERL Module library
+
#**With installation of Perl on windows, a utility called PPM(Perl Package Module) gets installed.
#* CPAN
+
#**Type ppm install module name.
#** Comprehensive Perl Archive Network is the library for modules.  
+
#'''PERL & HTML'''
#** We can search for require module in CPAN.
+
#* To create HTML pages, Perl provides CGI module which creates CGI script with require HTML tags.
#** If user is wrting his own module and wants to make it available to other Perl users also, he needs to upload it on CPAN.
+
#* 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.
#Adding path to a default list of paths for a module
+
#* @INC
+
#** If user has downloaded a module on a path of his choice and
+
#** wants to include this module into a perl program he needs to add that module into @INC array. #** This needs to be done at the start of program and can be achieved by using begin block;
+
#** <nowiki>begin {</nowiki><br><nowiki>unshift (@INC,"user/choice/path/of/file");</nowiki><br><nowiki>};</nowiki>
+
#Downloading required module onto windows or linux
+
#* Downloading CPAN module
+
#** Windows
+
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed.  
+
#*** This utility can be used to search and install the require module on windows environment.
+
#*** This also installs dependencies if any.
+
#** Linux: There are several ways on linux;
+
#*** type cpan and enter.
+
#**** This gives us cpan prompt.
+
#**** Type install ModuleName and it will install the module.
+
#*** perl -MCPAN -e 'install ModuleName';
+
# PERL & HTML
+
#* CGI module
+
#** To create html pages, perl provides CGI module
+
#** It 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.
+
 
+
==Advanced level== 
+
: '''Topics'''
+
#Function Prototyping
+
#Date & Time
+
#* Current Time Stamp
+
#* Various Date formats
+
#* Various operations that can be performed on date
+
#Oops in Perl
+
#* Object creation
+
#* Constructor
+
#* Destructor
+
#* Accessing methods using an object
+
#* Inheritance in Per
+
#Exporting functions in Perl
+
#* EXPORT
+
#* EXPORT_OK
+
#Pattern Matching / Regular expression in Perl
+
#* 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