Perl slid


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Perl slid

  1. 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. 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, 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
  3. 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. 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; Lets 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 16.  while(condition){ #statements; } $x = 0; while($x < 5){ print "$xn"; $x++; }
  17. 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. 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. 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. 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. 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.