I hope everyone who worked before in any computer language know about Variables. In perl also variables are same reserved memory location that store some data value that would probably will use in program. So here we have some data value that assigned through user or assigned while executing program which will further help in execution.

In Perl, one thing very interesting to know is according to data type variable assignment precedence get changed. As we know Perl has majorly three type of data type.

  1. Scalars
  2. Arrays
  3. hashes

So as said According to datatype precedence sign get change, like Scalars precedes by dollar ($) sign before variable. Arrays variables will precedence by at (@) sign. Hash variables precede by sign % and will be used to store sets of key/value pairs. Because we have different precedence in variables we can manage same name for two different datatype in same program.

Example of Variables

As we just mentioned type of variables in perl , let have some examples for various types of variables in Perl language.

Scalars variables Example

I hope everyone know how to assign values for variables. For this we will use assignment operator (=), sometime new comers get confused with equality operator (==). So let’s try to work with some variables examples like mentioned below.

root@jarvis:~/scripts/perl# ./variables_examples 
Enter your name: Sang
Enter your your age: 24
As you mentioned, Sang your age is: 24

How it run
root@jarvis:~/scripts/perl# cat variables_examples 
#!/usr/bin/perl

print "Enter your name: ";
#String variable example
chomp ($name=<STDIN>);

print "Enter your your age: ";
#Numeric variable example
$age=<STDIN>;

print "As you mentioned, $name your age is: $age";

As mentioned above we have define two type of variables, one is String (carried Name) and another is numeric (carried Age). Both has precedence of Dollar ($) before declaration.

Arrays Variable Example

Let try for Array variables examples.

#!/usr/bin/perl
print "Enter your name: ";
chomp ($name=<STDIN>);
print "Enter your your age: ";
chomp ($age=<STDIN>);

#print "As you mentioned, $name your age is: $age\n";

@country=("India", "China", "Japan", "Russia", "United kingdom", "United states", "Germany", "France\n");

print "Are you Belong to any of these country:\n\n  @country\n\n";

print "If not, Please provide your country name: ";

$country=<STDIN>;

print "\n\nAs you mentioned, $name you are $age years old and belong to $country\n";

So we just enlarge above program and manage to assign one Array and print it another line. let’s see how it runs.

# perl variables_examples 
Enter your name: Sang
Enter your your age: 24
Are you Belong to any of these country:

  India China Japan Russia United kingdom United states Germany France


If not, Please provide your country name: India


As you mentioned, Sang you are 24 years old and belong to India

I think you must absorb that new line character has really important in Perl. Here I used chomp method, basically it used to remove new line character just after variables provided through user.

Hash variable Example

So now i am re-phasing code again with some correction like adding “,” after country name and also adding another Hash type variable.Please note below code for Hash variable example.

#!/usr/bin/perl
print "Enter your name: ";
chomp ($name=<STDIN>);
print "Enter your your age: ";
chomp ($age=<STDIN>);

%country_leader=("India","Narendra Modi","China","Xi Jinping","Russia","Vladimir Putin","United Kingdom","Theresa May","United States","Donald Trump","Japan","Shinzō Abe","Germany","Angela Merkel");

@country=("India,", "China,", "Japan,", "Russia,", "United kingdom,", "United states,", "Germany,", "France\n");

#print "As you mentioned, $name your age is: $age\n";

print "Are you Belong to any of these country:\n\n  @country\n\n";

print "Please provide your country name: ";

chomp ($country=<STDIN>);

print "\n\nAs you mentioned, $name you are $age years old and belong to $country having country leader $country_leader{$country} \n";

Now we need to see how it present output when we run this code.

# perl variables_examples 
Enter your name: Sang
Enter your your age: 24
Are you Belong to any of these country:

  India, China, Japan, Russia, United kingdom, United states, Germany, France


Please provide your country name: Japan


As you mentioned, Sang you are 24 years old and belong to Japan having country leader Shinzō Abe 

With this we have all three types of examples for Perl variables and their use with program code with output. We will further discuss in detail in coming post for each variable type separately.

Next