SlideShare a Scribd company logo
1 of 22
 Introducing to Perl –
 Perl stands for Practical Extraction and Reporting Language. Perl
  is a general purpose, high level, interpreted and dynamic
  computer programming language with a vast number of uses.
 Invented By –
 Perl was invented by Larry Wall, a linguist working as a systems
  administrator at NASA in 1987. From the beginning, Perl was
  used as a general-purpose UNIX scripting language to help
  processing reports faster and easier. Since then Perl has
  undergone a lot of modification and improvements to become
  popular among developer community. Even though Perl has far
  surpassed its first invention, Larry Wall is still developing the
  core language with the newest version of Perl now is Perl.
 Use of Perl –
 Today Perl is found in varied applications such as
    finance, manufacturing, genetics, the military etc to process large data
    sets. Perl is used to be one of the most popular languages for
    developing web applications. At that time, Perl is used to write CGI
    scripts. You can find Perl in large projects such as Bugzilla, movable
    Type and other high-traffic websites such as Amazon.com, Ebay, Live
    Journal and Craigslist.
   Portability –
   Perl run well on UNIX and Windows systems. If you develop Perl for
    UNIX system, it can be portable to Windows system as well. This
    tutorial is only for Windows.
   Perl Development Environment
   Download and install Active Perl
   You first need to download Active Perl the latest version from
    http://www.activestate.com/activeperl/.
   First Program in Perl
   First, you open the Open Perl IDE and type the following code:
   #!/usr/bin/perl
   print "vande mataramn";
   You can save the Perl source code file with any name you want. It
    is highly recommended that Perl source code file should has
    extension .pl or .ple (Perl executable).
  When you click Run > Run or press F9, the program will launch
  and print the message “vande mataram”
 The first line of the program is a special comment. Comments in
  Perl program start from the pound sign (#) to the rest of line.
  There is no sign for block comment in Windows. On UNIX
  systems, two characters #! starting in a line indicates the
  program is stored in the file /usr/bin/perl
 Variables in perl
 Perl Scalar Variables
 Scalar data is the one of the most basic and simplest data in Perl. Scalar
    data can be number or string In Perl . Scalar variable starts with dollar
    sign ($) followed by Perl identifier. Perl identifier can contain
    alphanumeric and underscores. It is not allowed to start with a digit.
   $x = 3;
   Let's take a look in more details how we use scalar variables with
    number and string.
   Number
   Perl uses double-precision floating point values for calculation. Perl
    internally cheats integer as floating-point value. Perl uses literal to
    define number and with minus sign (-) to define negative number.
    Here is the code snippet to demonstrate scalar variable which holds
    number in Perl:
   #floating-point values
   $x = 3.14;

   $y = -2.78;


   #integer values
   $a = 1000;

   $b = -2000;
   Perl also accepts string literal as a number for example:
   $s = "2000"; # similar to $s = 2000;
   In above case $s can be use as a number when calculation even though
    it is a string.
 String
 Perl defines string as a sequence of characters. The shortest
    string contains no character or null string. The longest string can
    contain unlimited characters which is only limited to available
    memory of your computer. Similar to number, Perl represents
    string by literal. A string can be wrapped in a single or double
    quotes. For example:
   $str = "this is a string in Perl".
   $str2 = 'this is also as string too'.
   We defined two scalar variables which hold string in double
    quotes and sing quotes
   Operations on scalar variables
   Perl uses arithmetic operators as another languages like C/C++
    and Java. Here is the code snippet to demonstrate all of operators
    on numerical scalar variables.
   $x = 5 + 9; # Add 5 and 9, and then store the result in $x
   $x = 30 - 4;# Subtract 4 from 30
           # and then store the result in $x
   $x = 3 * 7; # Multiply 3 and 7 and then store the result in $x
   $x = 6 / 2; # Divide 6 by 2
   $x = 2 ** 8;# two to the power of 8
   $x = 3 % 2; # Remainder of 3 divided by 2
   $y = ++$x; # Increase $x by 1 and store $x in $y

   $y = $x++; # Store $x in $y then increase $x by 1
   $y = --$x; # Decrease $x by 1 and then store $x in $y
   $y = $x--; # Store $x in $y then decrease $x by 1
   $x = $y; # Assign $y to $x
   $x += $y; # Add $y to $x
   $x -= $y; # Subtract $y from $x
   $x .= $y; # Append $y onto $x
   For string Perl use full stop (.) for concatenating strings and (x)
    for repeating a string.
   x = 3;
   $c = "he ";
   $s = $c x $x;
   $b = "bye"; # $c repeated $x times
   print $s . "n"; #print s and start a new line
   # similar to
   print "$sn";
   $a = $s . $b; # Concatenate $s and $b
   For print
   $a = 10;
   Print $a;
   Perl If Statement
   In this tutorial, you will learn how to use Perl if control structure to
    write simple logic in code.
   Perl groups statements into block of code or block. A block is
    surrounded by a pair of curly braces and can be nested within a block.
   { #statements here
     { # nested block
       # statements here
     }
   }
 If control structure is used to execute a block of code based on a
    condition in Perl program. The syntax of If control structure is as
    follows:
   if(condition){
     statements;
   }
   If the condition is true the statements inside block will be
    executed, for example:
   $x = 10;
   $y = 10;
   if($x == $y){
     print "$x is equal to $y";
   }
 If you need an alternative choice, Perl provides if-else control
    structure as follows:
   if(condition){
     if-statements;
   }
   else{
     else-statements;
   }
   If the condition is false the else-statements will be executed.
    Here is the code example:
   $x = 5;
   $y = 10;
   if($x == $y)
    print "x is equal to y";
   }
   else{
     print "x is not equal to y";
   }
   Perl also provides if-else-if control structure to make multiple choices
    based on conditions.
   if(condition1){
   }
   else if(condition2){
   }
   else if(condition3){
   }
   ...
   else{
   }
 For loop
 you will learn how to interate a block of code multiple times by
    using Perl for loop statement.
   In order to run a block of code iteratively, you use Perl for
    statement. The Perl for statement is used to execute a piece of
    code over and over again. The Perl for statement is useful for
    running a piece of code in a specific number of times. The
    following illustrates Perl for statement syntax:
   1
   for(initialization; test; increment){
   2
     statements;
   3
   }
 There are three elements in the for statement - initialization, test and
    increment - are separated by semicolons. Perl does the following
    sequence actions:
   Step 1. The initialization is expression is evaluated - you can initialize
    counter variable here.
   Step 2. The test expression is evaluated. If it is true, the block -
    statements - will be executed.
   Step 3. After the block executed, the increment is performed and test is
    evaluated again. The process go to step 2 until the test expression is
    false. If it is never false, you encounter with a indefinite loop.
   Here is a code snippet to print a message 10 times.
   1
   for($counter = 1; $counter <= 10; $counter++){
   2
      print "for loop #$countern";
   3
   }
 Perl While loop

 you will learn another control flow statement called
  Perl while loop to execute a piece of code in a number
  of times based on a specific Boolean condition.
 Perl While statement is a Perl control structure that
  allows to execute a block of code repeatedly. The Perl
  while statement is used when you want to check a
  Boolean condition before making a loop.
 The following illustrates the Perl while statement
  syntax:
 while(condition){
     #statements;
 }
 $x = 0;
 while($x < 5){
 print "$xn";
     $x++;
 }
 List and Array variables
 Scalar variable allows you to store single element such
    as number or string. When you want to manage a
    collection or a list of scalar data you use list. Here are
    some examples of lists.
   ("Perl","array","tutorial");
   (5,7,9,10);
   (5,7,9,"Perl","list");
   (1..20);
   ();
 We have five lists. In the line 1 we have a list which
  contains three string. Line 2 we have a list which
  contains 4 integer. Line 3 we have a list which contains
  3 integer and 2 strings. Line 4 we have a list of 20
  elements. A pair of period (..) is called range operator.
  Line 5 we have an empty list. Each scalar data in the
  list is called list element.
 To store list to use throughout program you need array
  variable. Array variable is used to hold a list data.
  Different from scalar variable, array variable are
  prefixed with at sign(@). For example we have five
  array variables to store five lists above as follows:
 @str_array = ("Perl","array","tutorial");
 @int_array = (5,7,9,10);
 @mix_array = (5,7,9,"Perl","list");
 @rg_array = (1..20);
 @empty_array = ();
 Array element can be accessed by using indices starting
  from zero (0). Perl uses square bracket to specify the index.
  For example to get the second element of array @str_array
  we use expression as follows:
 $str_array[1];
 Be noticed that the dollar sign ($) is used instead of at sign
  (@). It is understandable that array element is scalar so ($)
  sign is used instead.
 Operations on array
 You can add or remove elements to/from an array. Here is a
    function list which allows you to do common operations on
    array:
   push(@array,$element) add $element to the end of array @array
   pop(@array) remove the last element of array @array and returns
    it.
   unshift(@array,$element) add $element to the start of array
    @array
   shift(@array) remove the first element from array @array and
    returns it.
   Here is the code snippet to demonstrate each function above:
 @int =(1,3,5,2);

 push(@int,10); #add 10 to @int
 print "@intn";

 $last = pop(@int); #remove 10 from @int
 print "@intn";

 unshift(@int,0); #add 0 to @int
 print "@intn";

 $start = shift(@int); # add 0 to @int
 print "@intn";
 Be noticed that the line 4, 7, 10 and 14 is used to print the array.
 VERY SOON WE COME WITH ADVANCE PERL.

More Related Content

What's hot

Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
Anil Dutt
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath
 

What's hot (20)

MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdfMANAGING INPUT AND OUTPUT OPERATIONS IN C    MRS.SOWMYA JYOTHI.pdf
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
Esoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programmingEsoft Metro Campus - Certificate in c / c++ programming
Esoft Metro Campus - Certificate in c / c++ programming
 
Data type in c
Data type in cData type in c
Data type in c
 
C Programming Assignment
C Programming AssignmentC Programming Assignment
C Programming Assignment
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
Strings-Computer programming
Strings-Computer programmingStrings-Computer programming
Strings-Computer programming
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
DITEC - Programming with C#.NET
DITEC - Programming with C#.NETDITEC - Programming with C#.NET
DITEC - Programming with C#.NET
 
Computer programming(CP)
Computer programming(CP)Computer programming(CP)
Computer programming(CP)
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 
C programming session 01
C programming session 01C programming session 01
C programming session 01
 
Functions-Computer programming
Functions-Computer programmingFunctions-Computer programming
Functions-Computer programming
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Functions, Strings ,Storage classes in C
 Functions, Strings ,Storage classes in C Functions, Strings ,Storage classes in C
Functions, Strings ,Storage classes in C
 

Viewers also liked

Ado.net session14
Ado.net session14Ado.net session14
Ado.net session14
Niit Care
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturte
Iblesoft
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegates
Iblesoft
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
Iblesoft
 

Viewers also liked (7)

Account's Assignment
Account's AssignmentAccount's Assignment
Account's Assignment
 
Ado.net session14
Ado.net session14Ado.net session14
Ado.net session14
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Microsoft.net architecturte
Microsoft.net architecturteMicrosoft.net architecturte
Microsoft.net architecturte
 
Controls
ControlsControls
Controls
 
Generics n delegates
Generics n delegatesGenerics n delegates
Generics n delegates
 
MS SQL Server 1
MS SQL Server 1MS SQL Server 1
MS SQL Server 1
 

Similar to Perl slid

Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
Shankar D
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
Mohammed Farrag
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
Elie Obeid
 

Similar to Perl slid (20)

newperl5
newperl5newperl5
newperl5
 
newperl5
newperl5newperl5
newperl5
 
perltut
perltutperltut
perltut
 
perltut
perltutperltut
perltut
 
Advanced perl finer points ,pack&amp;unpack,eval,files
Advanced perl   finer points ,pack&amp;unpack,eval,filesAdvanced perl   finer points ,pack&amp;unpack,eval,files
Advanced perl finer points ,pack&amp;unpack,eval,files
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1
 
What is the general format for a Try-Catch block Assume that amt l .docx
 What is the general format for a Try-Catch block  Assume that amt l .docx What is the general format for a Try-Catch block  Assume that amt l .docx
What is the general format for a Try-Catch block Assume that amt l .docx
 
Lecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administrationLecture 3 Perl & FreeBSD administration
Lecture 3 Perl & FreeBSD administration
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
Perl_Part4
Perl_Part4Perl_Part4
Perl_Part4
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Perl_Part6
Perl_Part6Perl_Part6
Perl_Part6
 
Complete Overview about PERL
Complete Overview about PERLComplete Overview about PERL
Complete Overview about PERL
 
Perl programming language
Perl programming languagePerl programming language
Perl programming language
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 

More from pacatarpit

More from pacatarpit (8)

Ado.net
Ado.netAdo.net
Ado.net
 
Sql slid
Sql slidSql slid
Sql slid
 
.Net slid
.Net slid.Net slid
.Net slid
 
Paca oops slid
Paca oops slidPaca oops slid
Paca oops slid
 
Paca java script slid
Paca java script slidPaca java script slid
Paca java script slid
 
Internet
InternetInternet
Internet
 
html tutorial
html tutorialhtml tutorial
html tutorial
 
What is c
What is cWhat is c
What is c
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Perl slid

  • 1.  Introducing to Perl –  Perl stands for Practical Extraction and Reporting Language. Perl is a general purpose, high level, interpreted and dynamic computer programming language with a vast number of uses.  Invented By –  Perl was invented by Larry Wall, a linguist working as a systems administrator at NASA in 1987. From the beginning, Perl was used as a general-purpose UNIX scripting language to help processing reports faster and easier. Since then Perl has undergone a lot of modification and improvements to become popular among developer community. Even though Perl has far surpassed its first invention, Larry Wall is still developing the core language with the newest version of Perl now is Perl.
  • 2.  Use of Perl –  Today Perl is found in varied applications such as finance, manufacturing, genetics, the military etc to process large data sets. Perl is used to be one of the most popular languages for developing web applications. At that time, Perl is used to write CGI scripts. You can find Perl in large projects such as Bugzilla, movable Type and other high-traffic websites such as Amazon.com, Ebay, Live Journal and Craigslist.  Portability –  Perl run well on UNIX and Windows systems. If you develop Perl for UNIX system, it can be portable to Windows system as well. This tutorial is only for Windows.  Perl Development Environment  Download and install Active Perl  You first need to download Active Perl the latest version from http://www.activestate.com/activeperl/.
  • 3. First Program in Perl  First, you open the Open Perl IDE and type the following code:  #!/usr/bin/perl  print "vande mataramn";  You can save the Perl source code file with any name you want. It is highly recommended that Perl source code file should has extension .pl or .ple (Perl executable). When you click Run > Run or press F9, the program will launch and print the message “vande mataram”  The first line of the program is a special comment. Comments in Perl program start from the pound sign (#) to the rest of line. There is no sign for block comment in Windows. On UNIX systems, two characters #! starting in a line indicates the program is stored in the file /usr/bin/perl
  • 4.  Variables in perl  Perl Scalar Variables  Scalar data is the one of the most basic and simplest data in Perl. Scalar data can be number or string In Perl . Scalar variable starts with dollar sign ($) followed by Perl identifier. Perl identifier can contain alphanumeric and underscores. It is not allowed to start with a digit.  $x = 3;  Let's take a look in more details how we use scalar variables with number and string.  Number  Perl uses double-precision floating point values for calculation. Perl internally cheats integer as floating-point value. Perl uses literal to define number and with minus sign (-) to define negative number. Here is the code snippet to demonstrate scalar variable which holds number in Perl:
  • 5. #floating-point values  $x = 3.14;   $y = -2.78;    #integer values  $a = 1000;   $b = -2000;  Perl also accepts string literal as a number for example:  $s = "2000"; # similar to $s = 2000;  In above case $s can be use as a number when calculation even though it is a string.
  • 6.  String  Perl defines string as a sequence of characters. The shortest string contains no character or null string. The longest string can contain unlimited characters which is only limited to available memory of your computer. Similar to number, Perl represents string by literal. A string can be wrapped in a single or double quotes. For example:  $str = "this is a string in Perl".  $str2 = 'this is also as string too'.  We defined two scalar variables which hold string in double quotes and sing quotes  Operations on scalar variables  Perl uses arithmetic operators as another languages like C/C++ and Java. Here is the code snippet to demonstrate all of operators on numerical scalar variables.
  • 7. $x = 5 + 9; # Add 5 and 9, and then store the result in $x  $x = 30 - 4;# Subtract 4 from 30  # and then store the result in $x  $x = 3 * 7; # Multiply 3 and 7 and then store the result in $x  $x = 6 / 2; # Divide 6 by 2  $x = 2 ** 8;# two to the power of 8  $x = 3 % 2; # Remainder of 3 divided by 2  $y = ++$x; # Increase $x by 1 and store $x in $y   $y = $x++; # Store $x in $y then increase $x by 1  $y = --$x; # Decrease $x by 1 and then store $x in $y  $y = $x--; # Store $x in $y then decrease $x by 1  $x = $y; # Assign $y to $x
  • 8. $x += $y; # Add $y to $x  $x -= $y; # Subtract $y from $x  $x .= $y; # Append $y onto $x  For string Perl use full stop (.) for concatenating strings and (x) for repeating a string.  x = 3;  $c = "he ";  $s = $c x $x;  $b = "bye"; # $c repeated $x times  print $s . "n"; #print s and start a new line  # similar to  print "$sn";  $a = $s . $b; # Concatenate $s and $b
  • 9. For print  $a = 10;  Print $a;  Perl If Statement  In this tutorial, you will learn how to use Perl if control structure to write simple logic in code.  Perl groups statements into block of code or block. A block is surrounded by a pair of curly braces and can be nested within a block.  { #statements here  { # nested block  # statements here  }  }
  • 10.  If control structure is used to execute a block of code based on a condition in Perl program. The syntax of If control structure is as follows:  if(condition){  statements;  }  If the condition is true the statements inside block will be executed, for example:  $x = 10;  $y = 10;  if($x == $y){  print "$x is equal to $y";  }
  • 11.  If you need an alternative choice, Perl provides if-else control structure as follows:  if(condition){  if-statements;  }  else{  else-statements;  }  If the condition is false the else-statements will be executed. Here is the code example:  $x = 5;  $y = 10;  if($x == $y)
  • 12. print "x is equal to y";  }  else{  print "x is not equal to y";  }  Perl also provides if-else-if control structure to make multiple choices based on conditions.  if(condition1){  }  else if(condition2){  }  else if(condition3){  }  ...  else{  }
  • 13.  For loop  you will learn how to interate a block of code multiple times by using Perl for loop statement.  In order to run a block of code iteratively, you use Perl for statement. The Perl for statement is used to execute a piece of code over and over again. The Perl for statement is useful for running a piece of code in a specific number of times. The following illustrates Perl for statement syntax:  1  for(initialization; test; increment){  2  statements;  3  }
  • 14.  There are three elements in the for statement - initialization, test and increment - are separated by semicolons. Perl does the following sequence actions:  Step 1. The initialization is expression is evaluated - you can initialize counter variable here.  Step 2. The test expression is evaluated. If it is true, the block - statements - will be executed.  Step 3. After the block executed, the increment is performed and test is evaluated again. The process go to step 2 until the test expression is false. If it is never false, you encounter with a indefinite loop.  Here is a code snippet to print a message 10 times.  1  for($counter = 1; $counter <= 10; $counter++){  2  print "for loop #$countern";  3  }
  • 15.  Perl While loop   you will learn another control flow statement called Perl while loop to execute a piece of code in a number of times based on a specific Boolean condition.  Perl While statement is a Perl control structure that allows to execute a block of code repeatedly. The Perl while statement is used when you want to check a Boolean condition before making a loop.  The following illustrates the Perl while statement syntax:
  • 16.  while(condition){  #statements;  }  $x = 0;  while($x < 5){  print "$xn";  $x++;  }
  • 17.  List and Array variables  Scalar variable allows you to store single element such as number or string. When you want to manage a collection or a list of scalar data you use list. Here are some examples of lists.  ("Perl","array","tutorial");  (5,7,9,10);  (5,7,9,"Perl","list");  (1..20);  ();
  • 18.  We have five lists. In the line 1 we have a list which contains three string. Line 2 we have a list which contains 4 integer. Line 3 we have a list which contains 3 integer and 2 strings. Line 4 we have a list of 20 elements. A pair of period (..) is called range operator. Line 5 we have an empty list. Each scalar data in the list is called list element.  To store list to use throughout program you need array variable. Array variable is used to hold a list data. Different from scalar variable, array variable are prefixed with at sign(@). For example we have five array variables to store five lists above as follows:
  • 19.  @str_array = ("Perl","array","tutorial");  @int_array = (5,7,9,10);  @mix_array = (5,7,9,"Perl","list");  @rg_array = (1..20);  @empty_array = ();  Array element can be accessed by using indices starting from zero (0). Perl uses square bracket to specify the index. For example to get the second element of array @str_array we use expression as follows:  $str_array[1];  Be noticed that the dollar sign ($) is used instead of at sign (@). It is understandable that array element is scalar so ($) sign is used instead.
  • 20.  Operations on array  You can add or remove elements to/from an array. Here is a function list which allows you to do common operations on array:  push(@array,$element) add $element to the end of array @array  pop(@array) remove the last element of array @array and returns it.  unshift(@array,$element) add $element to the start of array @array  shift(@array) remove the first element from array @array and returns it.  Here is the code snippet to demonstrate each function above:
  • 21.  @int =(1,3,5,2);  push(@int,10); #add 10 to @int  print "@intn";  $last = pop(@int); #remove 10 from @int  print "@intn";  unshift(@int,0); #add 0 to @int  print "@intn";  $start = shift(@int); # add 0 to @int  print "@intn";  Be noticed that the line 4, 7, 10 and 14 is used to print the array.
  • 22.  VERY SOON WE COME WITH ADVANCE PERL.