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


The output of this Perl script should shown as the video below:


Numeric Scalars

There are few types of numeric scalar such as integer, floating number, hex number, octal number, negative number and so on. Let's us try to code it as below:

#!/usr/bin/perl

$integer = 24;              # Store as integer
$float = 24.004;            # Store as float number
$negative = -320;           # Store as negative number
$hex = 0xFF;                 # Store as hex number
$octal = 0733;              # Store as octal number

print "Integer : $integer\n";
print "Float : $float\n";
print "Negative : $negative\n";
print "Hex : $hex\n";
print "Octal : $octal\n";

The output of this part is shown as the video below. You will notice that, the output of the hex and octal number will automatically convert into decimal.


String Scalars

In this part, we will demonstrates the usage of various types of string scalars. There is a difference between double quoted strings and single quoted strings. Let's us see how they will affect the output of the program.

#!/usr/bin/perl

$name = "Hooi Man Yeap";
$school = "SMK Seri Bedena";
$place = "Sungai Besar";

$example1 = 'My name is $name';
$example2 = '$school $place';

print "My name is $name\n";
print "$example1\n";
print "My secondary school is $school\n";
print "I come from $place\n";
print "$example2\n";

From this example, we noticed that the double-quoted string literals allows variable interpolation, whereas the single-quoted strings are not. The video below will what is the output of this example.


In the next tutorial, we will learn how the arrays data type perform in Perl language. See you guys in the next tutorial. :D

1 comment: