The Basic of Perl – 1 – Variable Declaration – Perl

In this small tutorial, I will write the basic of Perl that can help you learn Perl quickly.

First, in Perl there are variables. Variables are data storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value. In Perl, there are three main types of variables. An Array, a hash or a variable that can contain string, number, float, etc. To declare an array variable its name is preceding with the @ simple. For hash, the name is preceding with the % simple and for any other type of variable, the name is preceding with the $ symbol.

An array variable can store multiple values and is index base on numbers. The first index number in an array is 0. In this example (“a”, “b”, “c”), “a” string will be stored at index position 0, “b” string is stored in the second storage slot which is number one and the string “c” is stored in the third storage slot which is number 2. Just imagine that an array is a storage locker that only has one column and you can add as many storage boxes as needed. And within those storage boxes, you can store strings, numbers or even make a new storage locker.

A hash variable is somewhat similar to an array. Nevertheless, instead of having indexes as numbers, you actually can have a name address for the value. In others programming language, this is comparable to an object. In this example (“address” => “5555”, “address2” => “abc”), the “address” box will hold the value “5555” and “address2” storage box will hold the string value “abc”.

To declare a variable in Perl there are two keywords, “my” and “our”. When you declare any variables with the “our” keyword it is a global variable and can be accessed anywhere within the script. If you declare a variable with the “my” keyword you can only access it within the local scope.

This code below will demonstrate how to declare an empty variable.

our %hash = () ; # Declaring an empty global hash variable.
our @array = () ; # Declaring an empty global array variable.
our $variable = "" ; # Declaring an empty global variable. 

     sub empty(){
        my %hash1 = () ; # Declaring an empty local hash variable.
        my @array1 = () ; # Declaring an empty local array variable.
        my $variable1 = "" ; # Declaring an empty local variable. 
        # All local variables declare inside this sub will work here but not outside this scope, oh and can't forget global too.
     }
 
# Global variables will work here but not local variables. There is a way to call a local from another package which will be explained later.
# Nevertheless you can't call a local variable outside of it's function.

This code below is an example of declaring variables that contain information.

           
# %car is a has that have Make, Model and Color storage box
# Within the make box it would store a string with a value Nissan. 
my %car = ("Make" => "Nissan",
           "Model" => "Frontier",
           "Color" => "Black"
           ); # after any variable declaration there must be a semicolumn
           
# This is another way of declaring a hash. This style call pair value. 
# The first value is the name of the box and the second value is the content of the box.
# This way of declaring a hash can be confusing sometimes. It is best to avoid this way.
# Within the box, it's stored a string with a value brownfox.
my %hash = ("a", "brownfox",
            "d", "bluefox",
            "z", "horse"
            );

# @array is an array that store 3 values. In it's first index, it's store brownfox as index 0. In the second box it store bluefox at index 1 and so forth.
my @array = ("brownfox", "bluefox", "horse" );            
            
          
my $number = 13.5; # I am number variable with 13.5
my $binary = 0b1111 ; # I am a number variable but you declare me in binary mode. When I see 0b I assume you going to declare the binary block right after. I am 15.
my $hex = 0xFF ; # I am declared with hexadecimal value. When I see 0x I assume that the numbers behind 0x are hexadecimal in value. My value in decimal is 255.
my $octa = 01124; # I am an octa number, when I see 0 in front, I assume the rest of the numbers behind 0 are Octa value.
my $exp = 2.57e2 ; # I am a number declare with a scientific notation. 2.57 times 10 to the power of 2 which is 257.

my $string = "I am a string"; # This is a string variable. 
my $stringnumber = "01245"; # Don't think I am a number. I am a string that store number as characters.

This is the first post for The Basic of Perl, Please stay tuned for more post on The Basic of Perl.

This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "The Basic of Perl – 1 – Variable Declaration – Perl".
Original Post Link: http://kevinhng86.iblog.website/2017/01/18/the-basic-of-perl-1-variable-declaration-perl/.

Advertisement


Random Article You May Like

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*