Difference between revisions of "PERL"
(18 intermediate revisions by 4 users not shown) | |||
Line 9: | Line 9: | ||
''' | ''' | ||
− | The Spoken Tutorial Effort for '''Perl''' has | + | 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" |
− | # | + | #'''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 | ||
− | # | + | # '''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 | ||
− | |||
− | |||
#* 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 | ||
− | # | + | # '''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. | ||
− | |||
− | |||
#* 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 | ||
− | # | + | # '''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. | ||
− | |||
− | |||
#* 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. | ||
− | # | + | # '''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. | |
− | #* if-elsif-else | + | |
− | #** if-elsif-else statement is used to | + | |
− | + | ||
− | + | ||
#* switch Statement | #* switch Statement | ||
− | #** switch | + | #** switch is conditional case statement. Satisfied case gets execute else the default case gets execute. |
− | + | #'''Data Structures in Perl''' | |
− | + | ||
− | + | ||
− | + | ||
− | #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); | ||
− | #* | + | #* 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> | ||
− | # | + | # '''Arrays''' |
#* Getting Last index of array | #* 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 | #** 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. | #** 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 | ||
#** There are two ways to loop over an array | #** There are two ways to loop over an array | ||
#*** Using for loop | #*** Using for loop | ||
− | |||
#*** Using for-each loop | #*** Using for-each loop | ||
− | # | + | #''' 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. | ||
− | |||
#* 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. | ||
− | |||
#* sort | #* sort | ||
− | #** sorts the array in | + | #** 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 | ||
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 | ||
− | # | + | #'''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''' |
− | + | ||
#* 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. | ||
− | |||
#* 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. | ||
− | #** | + | #*UNITCHECK blocks |
+ | #*CHECK blocks | ||
+ | #*INIT blocks | ||
==Intermediate level== | ==Intermediate level== | ||
: '''Topics''' | : '''Topics''' | ||
− | #Access Modifiers in PERL | + | #'''Access Modifiers in PERL''' |
− | #* | + | #* private variable - my |
− | + | ||
#** scope is in the block inside where it is declared. | #** 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 | #** 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. | #** 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 | ||
− | #** | + | #** Get the actual entity being referred by reference. |
− | #** | + | #**Demo of various examples |
− | + | #'''Special Variables in PERL''' | |
− | #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. |
− | + | #'''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 |
− | #File Handling | + | #'''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() | |
− | #* | + | #'''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. |
− | #* | + | #'''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. | |
− | #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 |
− | #* | + | #'''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. | |
− | #* | + | #'''Downloading CPAN module''' |
− | + | #* Linux OS: | |
− | + | #**There are several ways to download. | |
− | #Perl | + | #**Type cpan and press Enter. |
− | #* | + | #**This gives us cpan prompt. |
− | #* | + | #**Type install module name. |
− | #* | + | #* Windows OS: |
− | #PERL Module library | + | #**With installation of Perl on windows, a utility called PPM(Perl Package Module) gets installed. |
− | + | #**Type ppm install module name. | |
− | # | + | #'''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. |
− | # | + | |
− | #* | + | |
− | #** | + | |
− | #** | + | |
− | #** | + | |
− | + | ||
− | # | + | |
− | + | ||
− | # | + | |
− | # | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | # | + | |
− | + | ||
− | # | + | |
− | + | ||
− | + | ||
− | # | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
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
- 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"
- Installation of Perl 5.14.2 on Ubuntu Linux
- 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’;
- 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
- Two types of comments -
- 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
- for Loop
- 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
- while Loop
- 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.
- if Statement
- 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.
- if-elsif-else statement
- 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’
);
- Scalar
- 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
- There are two ways to loop over an array
- 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.
- push
- 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
- keys
- Looping over a hash
- Functions in Perl
- Simple function
- Function with parameters
- Function which return single value
- Function which returns multiple values
- 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
- Begin
Intermediate level
- Topics
- 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.
- private variable - my
- 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
- Referencing
- 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.
- 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
- 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()
- 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.
- We can include the Perl modules or files by using the following methods.
- 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
- 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.
- 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.
- Linux OS:
- 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.