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.
 

Naming of a Variable

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
    

 



Declaration of a Variable

 
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 = (val1 = key1, val2 = key2, val3 = key3, …..);

    Example:

    %item_pairs = ("Apple" =>2, "Banana'=>3);
    %pair_random = ("Hi" =>8, "Bye"=>9);
    

 

Modification of a Variable

 
Perl allows to modify 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";
    
    # 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;
    

 

Variable Interpolation

 
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 same but there exists a slight difference between how they work. Strings which 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 displays the string. It even replaces the escape sequences by their real use.

Example:

$name = "John"
print "Hi $name\nHow are you?" 

The above code will print:

Hi John
How are you?

Example Code:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/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);

chevron_right


Output:

Modified Array is G E F K S
Name is $name\n
Name is GeeksForGeeks$VAR1 = {
          'to' => 20,
          'Welcome' => 10,
          'Geeks' => 40
        };


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.