Perl | Variables
Variables in Perl are used to store and manipulate data throughout the program. When a variable is created it occupies memory space. The data type of a variable helps the interpreter to allocate memory and decide what to be stored in the reserved memory. Therefore, variables can store integers, decimals, or strings with the assignment of different data types to the variables.
A variable in Perl can be named anything with the use of a specific datatype. There are some rules to follow while naming a variable:
- Variables in Perl are case-sensitive.
Example:
$John and $john are two different variables
- It starts with $, @ or % as per the datatype required, followed by zero or more letters, underscores, and digits
- Variables in Perl cannot contain white spaces or any other special character except underscore.
Example:
$my-name = "John"; // Invalid $my name = "John"; // Invalid $my_name = "John"; // Valid
Variable Declaration is done on the basis of the datatype used to define the variable. These variables can be of three different datatypes:
- Scalar Variables: It contains a single string or numeric value. It starts with $ symbol.
Syntax: $var_name = value;
Example:
$item = "Hello" $item_one = 2
- Array Variables: It contains a randomly ordered set of values. It starts with @ symbol.
Syntax : @var_name = (val1, val2, val3, …..);
Example:
@price_list = (70, 30, 40); @name_list = ("Apple", "Banana", "Guava");
- Hash Variables: It contains (key, value) pair efficiently accessed per key. It starts with % symbol.
Syntax : %var_name = ( key1=>val1, key2=>val2, key3=>val3, …..);
Example:
%item_pairs = ("Apple" =>2, "Banana'=>3); %pair_random = ("Hi" =>8, "Bye"=>9);
Perl allows modifying its variable values anytime after the variable declaration is done. There are various ways for the modification of a variable:
- A scalar variable can be modified simply by redefining its value.
Example:
$name = "John"; # This can be modified by simply # redeclaring the variable $name. $name = "Rahul";
- An element of an array can be modified by passing the index of that element to the array and defining a new value to it.
Example:
@array = ("A", "B", "C", "D", "E"); # If value of second variable is to # be modified then it can be done by @array[2] = "4"; # $array[2] = "4"; is an alternate way of updating value in an array. # This will change the array to, # @array = ("A", "B", "4", "D", "E");
- A value in a hash can be modified by using its Key.
Example:
%Hash = ("A", 10, "B", 20, "C", 30) # This will modify the value # assigned to Key 'B' $Hash{"B"} = 46;
Perl provides various methods to define a String to a variable. This can be done with the use of single quotes, double quotes, using q-operator and double-q operator, etc.
Using single quotes and double quotes for writing strings is the same but there exists a slight difference between how they work. Strings that are written with the use of single quotes display the content written within it exactly as it is.
Example:
$name = "John" print 'Hi $name\nHow are you?'
The above code will print:
Hi $name\nHow are you?
Whereas strings written within double quotes replace the variables with their value and then display the string. It even replaces the escape sequences with their real use.
Example:
$name = "John" print "Hi $name\nHow are you?"
The above code will print:
Hi John How are you?
Example Code:
Perl
#!/usr/bin/perl use Data::Dumper; # Scalar Variable $name = "GeeksForGeeks" ; # Array Variable @array = ( "G" , "E" , "E" , "K" , "S" ); # Hash Variable %Hash = ( 'Welcome' , 10, 'to' , 20, 'Geeks' , 40); # Variable Modification @array [2] = "F" ; print "Modified Array is @array\n" ; # Interpolation of a Variable # Using Single Quote print 'Name is $name\n' ; # Using Double Quotes print "\nName is $name" ; # Printing hash contents print Dumper(\ %Hash ); |
Modified Array is G E F K S Name is $name\n Name is GeeksForGeeks$VAR1 = { 'to' => 20, 'Welcome' => 10, 'Geeks' => 40 };