Sunday, January 25, 2015

Tutorial 9 : Operator Example

In this tutorial, we will learn all kind of kind of the operators which is exists in Perl language. These kind of operators are powerful to us, especially for programmer. It help us to solve a lot of problems and able to perform variety kind of operation. Below is the list of important operators which are existing in Perl language:
  • Arithmetic Operators
  • Equality Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Logical Operators
  • Quote-like Operators
  • Miscellaneous Operators

Arithmetic Operators

The table at below shows all the arithmetic operators which we can use in Perl.

Table 9.1: Perl Arithmetic Operators



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";