Perl variable

A variable is data stored in memory. Creating a variable creates a space in memory.

The interpreter determines the amount of memory in memory based on its type, so you can assign different data types to variables, such as integers, floats, strings, and more.

In the previous chapter, we introduced you to the three basic data types of Perl: scalars, arrays, and hashes.

    The scalar $ starts, as $a $b is two scalars.
  • The array @start, as @a @b is two arrays.
  • Hash % starts, %a %b is two hashes.

Perl sets a separate command space for each variable type, so different types of variables can use the same name, and you don't have to worry about conflicts. For example, $foo and @foo are two different variables.

Create a variable

Variables do not need to explicitly declare a type. After the variable is assigned, the interpreter automatically allocates the matching type space.

Variables are assigned with an equal sign (=).

We can use the use strict statement in our program to make all variables need to be declared.

The left side of the equal sign is a variable, and the right side is a value. The example is as follows:

$age = 25; # Type
$name = "javacodegeeks"; # string
$salary = 1445.50; #float

In the above code 25, "javacodegeeks" and 1445.50 are assigned to $age, $name and $salary variables respectively.

Next we will see the use of arrays and hashes.

scalar variable

A scalar is a single unit of data. Data can be integers, floats, characters, strings, paragraphs, and more. Simply put it can be anything. Here's a simple application of scalars:


#!/usr/bin/perl $age= 25 ; # Integer $name = " javacodegeeks"; #string $salary= 1445.50 ; # floats print "Age = $age\n"; print "Name = $name\n"; print "Salary = $salary\n";

The above program execution output is:

Age = 25
Name = javacodegeeks
Salary = 1445.5

Array variable

Arrays are variables used to store an ordered scalar value.

Array @ Start.

To access an array of variables, you can use the dollar sign ($) + variable name and specify the subscript to access, the example is as follows:


#!/usr/bin/perl @ages = ( 25, 30, 40) ; @names= ( "google", "javacodegeeks ", "welookups "); print "\$ages[0] = $ages[0]\n"; print "\$ages[1] = $ages[1]\n"; print "\$ages[2] = $ages[2]\n"; print "\$names[0] = $names[0]\n"; print "\$names[1] = $names[1]\n"; print "\$names[2] = $names[2]\n";

The above program execution output is:

$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = google
$names[1] = javacodegeeks
$names[2] = welookups

In the program we used the escape character (\) before the $ tag to output the character $.

Hash variable

Hash is a collection of key/value pairs.

Hash % starts.

If you want to access a hash, you can use the $ + {key} format to access:


#!/usr/bin/perl %data = ( 'google', 45, 'javacodegeeks', 30, 'welookups', 40); print "\$data{'google'} = $data{'google'}\n"; print "\$data{'javacodegeeks'} = $data{'javacodegeeks'}\n"; print "\$data{'welookups'} = $data{'welookups'}\n";

The above program execution output is:

$data{'google '} = 45
$data{'javacodegeeks'} = 30
$data{'welookups'} = 40

Variable context

The so-called context: refers to the location of the expression.

The context is determined by the variable type to the left of the equal sign. The left side of the equal sign is a scalar, which is the scalar context. The left side of the equal sign is the list, which is the list context.

The Perl interpreter determines the type of the variable based on the context. An example is as follows:


#!/usr/bin/perl @names= ( 'google', 'javacodegeeks ', 'welookups '); @copy = @names; # Copy Array $size = @names; # Array assignment to scalar, return the number of array elements print "The name is: @copy\n"; print "The number of names is: $size\n";

The above program execution output is:

Name is : google javacodegeeks welookups
The number of names is  : 3

In the code @names is an array that is applied in two different contexts. The first one copies it to another array, so it outputs all the elements of the array. The second we assign the array to a scalar that returns the number of elements in the array.

A variety of different contexts are listed below:

welookups is optimized for learning.© welookups. 2018 - 2020 All Right Reserved and you agree to have read and accepted our term and condition.