Overview Of Perl Basic Data Types

03:00

Before we begin with learning of Perl coding programs you must be familiar with what data types Perl support? Without knowing data type you cannot go any further in Perl programming. Let’s have a look at data type provided by Perl.





Perl is loosely type language and there is no need to specify the type for the variable. The Perl interpreter automatically selects the data type based on context of data itself. Perl support 3 Basic data types:
  • Scalar:
    Scalar is simple data type you can use in Perl programming. They are used as variable in the program. It is easy to identify Scalar type since they start with $ sign as prefix. A scalar type can hold the value of String, Number or a reference of variable. They are mostly used as variables and they have their scope limits too. We will see in details in upcoming chapters.
    Example :
    $a="abc";    #String Scalar
    $num=10;         #Numerical Scalar
    $b=$$a;      #It store reference of a in scalar variable $b
  • Arrays:
    Arrays are ordered list of scalar that can access of data in numerical index sequence start from 0.Arrays also can be easily identified since they start with @ sign as prefix. You can also access their particular index value using Scalar types.
    Example:
    @a={“ABC”,”XYZ”};    #Array contain String value
  • Hashes:
    Hashes are unordered list of Key and value pair. In hashes data can be access using key as subscript. They are also easy to identify them since they start with % sign as prefix.
    Example:
    %data = ('John Paul', 45, 'Lisa', 30, 'Kumar', 40);
  •  
Numeric Literals
Perl store numeric literals internally as signed integers or double precision floating point values. Numeric literals are as follows in Perl.
Type
Value
Integer
1234
Negative Int
-1234
Scientific Notation
12.6E14
Octal
0xffff
Hexadecimal
0577

Escape Sequence:
Escape sequences are special character then used in any other language like HTML and other languages. But you have to user it with double quote “” or else it will not be interpolated. Here is list commonly of escape sequences and their work:
Escape sequence
Functioning
\\
Backslash Character
\a
Alert or beep sound
\b
Backspace character
\n
Newline character
\u
Force next character to uppercase
\U
Force all following characters to uppercase
\l
Force next character to lowercase
\L
Force all following characters to lowercase
\t
Horizontal tab
\v
Vertical tab

Example:
Make one file like with name you like and make sure it’s extension as .pl
#!/usr/bin/perl

use strict;
use warrning;

#this is interpolated exmaple of new line character .
$welcome="Welcome to \n http://www.nothinglikeeverything.com/";

#this is non-interpolated example of newline character.
$welcome='Welcome to \n http://www.nothinglikeeverything.com/';

#This statement will produce beep
$beep="\a This statement will cause beep";

#This make enitre statement capital formated
$str="\U Hi user I hope you are undertanding what I am doing";

#This make enite statement in lowrcase format
$lower="\L HI USER I KNOW BIT DIFFCULT BUT IT ALL MAKE SENSE AT THE END";

#This will make horizontal and verical tab space
$space="Here is \t Hirizontal tab \t and here is \v verticle tab ";

OUTPUT:
Welcome to
 http://www.nothinglikeeverything.com/
Welcome to \n http://www.nothinglikeeverything.com/ This statement will cause beep
 HI USER I HOPE YOU ARE UNDERTANDING WHAT I AM DOING
 hi user i know bit diffcult but it all make sense at the end
Here is          Hirizontal tab   

Share this

Related Posts

Previous
Next Post »