Navigation Menu

Approach to Perl Programming



What is Perl?

Perl is a general purpose programming language originally developed for text manipulation. Now it is also used for Web development, network programming, GUI development and more.

Perl is an acronym for Practical Extracted and Report Language. Perl works with XML, HTML and other mark-up languages. Perl interfaces with external C/C++ libraries.


Perl is Interpreted

Perl is interpreted language, which means that your code can be run as is without a compilation stage.

Traditional compilers convert programs into machine language. When you run a perl program, it’s first compiled into a byte code, which is then converted to machine instructions.


Perl Environment Setup

Step1: Check whether perl already installed into your system.


 Step2: Goto http://strawberryperl.com/ to download if it is not installed.



Step3: Install the downloaded file.




Step4: Make sure that installation completed successfully.



Writing a First Perl Program

Step1: Goto the folder where perl software was installed.



Step2: Create a document with extension as .pl.


Step3: Write the starting program in the document.




Step4: Run the document using command prompt.




Creating Variables

Perl variables do not have to be explicitly declared to reserve memory space. The declaration happens automatically when you assign a value to a variable.


‘$’ sign is used to declare a variable.

Example:


Output:




Array Variables

Array variable is prefixed with '@' operator and populated using parenthesis or qw operator.

Example:




Conditional Statements

1. if() statement:



Output:




2. if..else() statement:


Output:




Looping Statements

1. for() loop:



Output:




2. while() loop:

Output:




3. do..while() loop:



Output:




4. foreach() loop:




String Manipulations

1. Concatination:



Output:



2. Converting to Uppercase and Lowercase:


Output:




2. Finding a Substring:




3. Index():

This function position of -1 if no match found or else returns position of matching string.




4. Split():




5. Join():





Interacting to File Systems


Example1: Opening and writing to a file.



Example2: Finding and replacing the text on file.





Example2: Reading and displaying text from a file.



Input file and Output:











0 comments: