Saturday, November 1, 2014

Introduction to Perl

This guide introduces the Perl language through a number of sample programs. The aim of this guide is to understand how to use regular expressions with Perl.

1 - What is Perl Language

Perl is a scripting language suitable for web-processing. Perl was developed deriving its concepts of text handling and regular expression from two specialized languages called awk and sed.

2 - Types of Data and Variables

- Numbers: Perl normally makes no distinction between integers and floating point numbers.
- Strings: a string literal is a sequence of characters enclosed in single quotes or in double quotes.
- Variables: a variable is a name for a container that holds a value. A variable name starts with a dollar sign and can contain any value: number and text.

Let's look at a simple example:
 $centimeters = 30;
 # conversion into inches
 $inches = $centimeters / 2.54;
 # report both lengths
 print "$centimeters cm is $inches in.\n";
When executed, this produces:
 max$ perl -w example.pl
 30 cm is 11.8110236220472 in.
The -w option that tells Perl to issue warnings about what might be mistakes.

We can draw the following grammar rules:
- Comments begin with # and continue for the rest of the line.
- In contrast with many languages, variables can appear within a double-quoted string. With the string "$centimeters cm is $inches in.\n", each variable is replaced by its value and the result is printed.

3 - Control Structures

Example showing the if structure:
 $temperature = 22;
 print "temperature is $temperature.\n";
 if($temperature < 20) {
   print "It's cold\n";
 } elsif($temperature > 26) {
   print "It's hot\n";
 } else {
   print "I feel good\n";
 }
When executed, this produces:
 max$ perl -w example_2.pl
 temperature is 22.
 I feel good

Example showing the while loop:
 $temperature = 10;
 while ($temperature < 40) {
   print "temperature is $temperature C°\n";
   $temperature = $temperature + 5;  
 }
When executed, this produces:
 max$ perl -w example_3.pl
 temperature is 10 C°
 temperature is 15 C°
 temperature is 20 C°
 temperature is 25 C°
 temperature is 30 C°
 temperature is 35 C°

4 - Equality Operators

if (20 == 20) { 
  print "== for numeric values\n"; 
} 
if ('boat' eq 'boat') { 
  print "eq for string values\n"; 
}

5 - Getting User Input

When a Perl program encounter the <STDIN> operator, it reads a complete text line from standard input and uses that string as the value of <STDIN>. Generally standard input means the keyboard of the user who invoked your program.

The string value of <STDIN> typically has a newline character on the end of it. In practice, you don’t often want to keep the newline, so you use the chomp() operator. The chomp() operator works on a variable holding a string and removes the trailing newline character from the string.
 $text = <STDIN>; # Read text from <STDIN>
 chomp($text);   # Gets rid of the newline character
The most common use of chomp() looks like this:
 chomp($text = <STDIN>); # Read the text, without the newline character

6 - Formatted Output with printf

The printf operator prints a formatted string to current output. The printf operator takes a format string followed by a list of things to print: printf FORMAT, LIST
 $customer = 'John Belushi';
 $balance = '19.05';
 $days = '21';
 printf "Dear %s, your credit balance is %2.2f € and expires in %d days\n", 
  $customer, $balance, $days;
When it runs, you get:
 Dear John Belushi, your credit balance is 19.05 € and expires in 21 days

7 - Matching text with regular expressions

The simplest way to use a regular expression with Perl is matching a text held in a variable. This snippet checks the string held in variable $reply and reports whether it contains only digits:
 if ($input =~ m/^[0-9]+$/) {
   print "only digits\n";
 } else {
   print "not only digits\n";
 }
Perl try to match ^[0-9]+$ with the content of the variable $input:
- the match operator m/regex/ tells Perl to attempt a regular expression match.
- the binding operator =~ links a regex search with the target string on the left.

The result of $input =~ m/^[0-9]+$/ is a true value if the ^[0-9]+$ matches the string held in $input, a false value otherwise.

No comments :

Post a Comment