Difference between revisions of "PERL"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 43: Line 43:
 
#** =cut =head or =begin =end
 
#** =cut =head or =begin =end
 
#**Start with = sign
 
#**Start with = sign
#:<br>
 
 
# '''for-foreach-Loop'''
 
# '''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
 
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-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>
 
#** 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'''
 
# '''Conditional 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:<br><nowiki>$count = 5;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>}</nowiki>
 
 
#* 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>#executes this piece of code if the above if condition is evaluates to false.</nowiki><br><nowiki>Another piece of code;</nowiki><br><nowiki>}</nowiki>
 
#** eg:<br><nowiki>$count = 4;</nowiki><br><nowiki>if ($count == 5) {</nowiki><br><nowiki>print "I am inside if\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am inside else\n";</nowiki><br><nowiki>}</nowiki>
 
 
# '''More Conditional Statements'''
 
# '''More Conditional 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.
+
#** 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.
#** 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:<br><nowiki>$language = 'Perl';</nowiki><br><nowiki>if ($language eq 'Perl') {</nowiki><br><nowiki>print "Hi, I am Perl\n";</nowiki><br><nowiki>} elsif ($language eq 'Java') {</nowiki><br><nowiki>print "Hi, I am Java\n";</nowiki><br><nowiki>} else {</nowiki><br><nowiki>print "I am not a computer language\n";</nowiki><br><nowiki>}</nowiki>
+
 
#* 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.
#** 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 = 'Perl';</nowiki><br><nowiki>switch ($var){</nowiki><br><nowiki>case 'Perl' {print “I am Perl\n”;}</nowiki><br><nowiki>case 'Java' {print “I am Java\n”;}</nowiki><br> <nowiki>case 'Linux' {prin “I am Linux\n”;}</nowiki><br><nowiki>else {print “I am not a computer language\n”;}</nowiki><br><nowiki>}</nowiki><br>
+
 
#'''Data Structures in Perl'''
 
#'''Data Structures in Perl'''
 
#* Scalar
 
#* Scalar
Line 101: Line 81:
 
# '''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'''
 
#''' Array functions'''
 
#* push
 
#* push
Line 127: 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 alphabatical order.
 
# '''Hash in Perl'''
 
# '''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 145: 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
 
#*UNITCHECK blocks
 
#*CHECK blocks  
 
#*CHECK blocks  
Line 171: Line 134:
 
: '''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.
 
#* Special variables have a predefined and special meaning in Perl.
 
#* These variables are denoted by usual variable indicator such as $, @, % along with punctuation characters.  
 
#* These variables are denoted by usual variable indicator such as $, @, % along with punctuation characters.  
 
#'''File Handling'''
 
#'''File Handling'''
#* To open a file  
+
#* Open a file  
#** open FH, FileName.txt;  # FH is the file handle
+
#* Open a File in Read Mode
#* To open a file in read mode
+
#* Open a File in Write Mode
#** open FH, "<FileName.txt";
+
#* Open a File in Append Mode
#* To open a file in read, write mode
+
#* Close the FileHandle
#** open FH, ">FileName.txt";
+
#* To open a file in append mode
+
#** open FH, ">>FileName.txt";
+
#* Looping on each line can be done as
+
#** eg:<br><nowiki>while (<FH>) {</nowiki><br><nowiki>print "$_"; # prints each line of a file</nowiki><br><nowiki>}</nowiki>
+
#* write into a file
+
#** print FH "I am writing in a file";
+
#* close the file handle
+
#** close FH
+
 
#'''Exception and error handling in PERL'''  
 
#'''Exception and error handling in PERL'''  
#* eval block in perl
+
#* When an error occurs, exception and error handling helps to recover the program.
#** User can use eval, if he wants to execute a piece of code even if there is a error / exception
+
#*Methods used in Perl:
#** or to know exactly where the code is failing with the type of error.
+
#** warn()
#** 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>
+
#** die()
 +
#** eval()
 
#'''Including files and/or modules in a PERL program'''
 
#'''Including files and/or modules in a PERL program'''
#* do
+
#* We can include the Perl modules or files by using the following methods.
#** it includes a file without checking that the file is already included or not
+
#** do: It includes the source code from other files into the current script file.
#* require
+
#*use: It includes Perl module files only. Files get included before the actual execution of the code.
#** it includes a file if it not included already.
+
#**  require: It includes both Perl programs and modules.
#* use
+
#** it includes perl module files only.  
+
#** Files get included before the actual execution of the code gets start.
+
 
#'''Sample Perl Programs'''
 
#'''Sample Perl Programs'''
 
#*Includes all major topics that we covered so far in this sample program.
 
#*Includes all major topics that we covered so far in this sample program.
Line 229: Line 174:
 
#* weather_report.pl is the Perl program which makes use of this module file to give the required output
 
#* weather_report.pl is the Perl program which makes use of this module file to give the required output
 
#'''PERL Module library(CPAN)'''
 
#'''PERL Module library(CPAN)'''
#* These are the .pm files.
+
#* Comprehensive Perl Archive Network (CPAN) is the library of modules.
#* Modules can be used to reuse piece of code written earlier by someone and serves the user requirement.
+
#** User can make use of the existing modules available in CPAN
#* 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.
+
#** New modules created by the user can be uploaded to CPAN so that other Perl users can make use of it.
#* CPAN
+
#** Comprehensive Perl Archive Network is the library for modules.  
+
#** We can search for require module in CPAN.
+
#** 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.
+
#* 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 CPAN module'''
 
#'''Downloading CPAN module'''
#** Windows
+
#* Linux OS:
#*** With installation of PERL on windows, a utility called PPM (Perl Package Module) gets installed.
+
#**There are several ways to download.
#*** This utility can be used to search and install the require module on windows environment.
+
#**Type cpan and press Enter.
#*** This also installs dependencies if any.
+
#**This gives us cpan prompt.
#** Linux: There are several ways on linux;
+
#**Type install module name.
#*** type cpan and enter.  
+
#* Windows OS:
#**** This gives us cpan prompt.  
+
#**With installation of Perl on windows, a utility called PPM(Perl Package Module) gets installed.
#**** Type install ModuleName and it will install the module.
+
#**Type ppm install module name.
#*** perl -MCPAN -e 'install ModuleName';
+
 
#'''PERL & HTML'''
 
#'''PERL & HTML'''
 
#* To create HTML pages, Perl provides CGI module which creates CGI script with require HTML tags.
 
#* 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.
 
#* 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.

Revision as of 17:00, 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);
    • 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’
        );
  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 alphabatical 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

  1. 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.
  2. 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
  3. 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()
  4. 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.
  5. 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
  6. 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.
  7. 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.
  8. 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