Sunday, December 28, 2014

Tutorial 8 : Branch Example

In the last tutorial, we learn loop statement in Perl language. In this tutorial, we will show you how we can do interrupt process in the loop statement. Below is the few commands we can use in Perl.

Next Statement

Next statement will start the next iteration of the loop. It can be used inside a nested loop or perform outside of the loop. Let's us see how it can be perform.

Next statement flow

#!/usr/bin/perl

for($i=0; $i<10; $i++)
{
  if($i==5)  {next;}
  print "Value = $i\n";
}

Saturday, December 27, 2014

Tutorial 7 : Loop Example

In this tutorial, we will learn how the loop statement perform in Perl language. Loop statement is used when we need to execute a block code for several number of times. Let's us see which command we can use at this part.

While Loop

This is command will check the condition first before enter the loop. If the condition is true, it will execute the statement in the loop. If not, program control passes to the line immediately after the loop. Let's us see the example below.

While loop flow

Tutorial 6 : Condition Example

In this tutorial, we will learn few common condition statement which is helps in decision making. Normally these kind of conditions is used to evaluated in program. If the condition is true, it will execute the following statement; if false, it will execute another statement. Let's start to learn how to use it.

If Statement

In this case, if the boolean expression is true, then the block code in the if statement will be executed. While don't care it is true or not, it will execute the block code after the if statement too. Below is the example of this case.

IF condition statement flow.

#!/usr/bin/perl

$money = 100;

if ($money==100)
{
  print "I have 100 dollar\n";
}

print "I don't have 100 dollar\n";

Tutorial 5 : Hashes Example

Hey, guys. Welcome back to Cer tutorial. In this tutorial, we will learn the last data type which is existing in Perl language. Hash is a set of key and value pair. This variable is come before with a percent (%) sign. It is very convenient for us, especially get the reference value from the key. 

What is 'key' and 'value'? Key is a variable / memory which is used to store the data / value. When you call the particular key, it will return the stored value for the system. Let's us see how it works.

Hashes

Create and Access Hashes

There are two ways to create hashes. The first method is assign a value to a key with one-by-one basic. Second method is use a list, the first element will be used as the key and second element will be the value of the key. Let's see how it works.

#!/usr/bin/perl

$name{'John'} = 45;           # one-by-one basic
$name{'Ali'} = 50;
%data = ('Car' => 40, 'House' => 100);   # list basic

print "John is $name{'John'} years old\n";
print "Ali is $name{'Ali'} years old\n";
print "Ali is older than John\n";
print "John own a car which is cost $data{'Car'}k\n";
print "Whereas Ali own a house which is cost $data{'House'}k\n";

Tutorial 4 : Arrays Example

In this tutorial, we will show you how to perform arrays data type in Perl language. Beside that, we also will show you what is the special function can be done by it.

Arrays

Variable Arrays

Arrays is a variable which can store an ordered list of scalar values. In Perl language, the arrays variables come before by an "at" (@) sign. When we want to refer to a single element of the array, we can use the dollar sign ($) with the variable name and followed by the index of the element in square brackets ([index]). Below is the example to show you how to use an arrays variable.

#!/usr/bin/perl

@month = ("January","February","March");
@day = (1,2,3,4,5,6,7);

print "Today is $day[0]st $month[0] 2014\n";
print "My friend's birthday on $day[2]rd of $month[2] 1990\n";

The output of this example is shown as video below:


Friday, December 26, 2014

Tutorial 3 : Scalar Example

Hi, guys. In this tutorial, we will learn the data types in Perl language. Basically, there are three data types: scalars, arrays of scalars and hashes of scalars. There is a big different between Perl language and others language (e.g. C language, Java language, ...). Since Perl language is a loosely typed language, therefore it doesn't need to define the type of data (e.g. integer, string, character, ...) when using in the program, while others do so before using it.

We will store these data type as variable before we use it. What is variable? Variable is the reserved memory locations to store values. These values can be integers, strings or decimals. The Perl interpreter will allocates the memory and make decisions what will be stored in the reserved memory.

In this tutorial, we will learn the first data type which is scalar.

Scalars

Variable Scalars

A scalar is a single unit data. This data can be an integer number, double point, floating point, a string or a character. Or in other word, it can be anything, but only can store single thing. This scalar variable will be come before by a dollar sign ($). Below is the example using scalar variable.

#!/usr/bin/perl

$name = "Man Yeap";     # Store as String
$age = 24;              # Store as integer
$gender = 'M';          # Store as character

print "Name : $name\n";
print "Age : $age\n";
print "Gender : $gender\n";

Friday, December 12, 2014

Tutorial 2 : First Perl Program

Perl programming is a sequence programming language. It read and run the program from top to the bottom of script. Besides, it supports subroutines, loops and control structure which make the program more flexible. Every statement line must be end with a semicolon (;), if not the script will encounter compile error due to syntax error.

Perl Installation

Before we start to writing our first Perl program, let's setup our Perl environment in the system. First come first, Perl is supported in various of operating system (OS). The OS which are commonly used by the most users are listed at below:
  • Unix (Linux, SunOS, etc.)
  • Window (9x/NT/2000/XP/Vista)
  • Debian GNU
  • Others OS
In this series of tutorials, I would like to use Linux OS. But don't worry about it, the language still are the same just the GUI interfacing might be different. In a general sense, when you installing Linux OS into your desktop / laptop, the Perl environment already been provided in the system. To be confirm whether it is already supported in the system or which version are been used, you can simply open the terminal to check for it. 

Saturday, December 6, 2014

Tutorial 1 : Introduction of Perl

Perl is powerful language which is able to be supported by different kind of operating system. It is a high level and general-purpose programming language. Now, it is widely used in various tasks such as web development, system administration, Graphical User Interface (GUI) development, network & security programming.

Figure 1.1: Logo of Programming Republic of Perl.

Benefits of Perl Language

  •  Perl is an Open Source language which is licensed under General Public
     Licence (GPL).
  •  Perl also is a powerful, stable and cross platform programming language
     which able to support different kind of Operating System (OS) such as
     Unix, Window or Mac.
  •  Perl able to borrow the features from other languages, e.g. C, AWK,
     Shell,  BASIC and others.
  •  Perl language also supports Unicode.
  •  Perl is extensible language. It able to support over 20,000 third party
     modules which are available from the Comprehensive Perl Archive
     Network (CPAN).
  •  Perl able to be work with other mark-up languages, e.g. HTML, PHP,
     XML and so on. It aslo commonly used to support for the database
     integration interface such as MySQL, Oracle and others.

Let's us start to study the Perl Language and see what things can be done by it. If you have the basic of the other programming language, it is easier for you to catch up.