A variable is data stored in memory, and creating a variable opens up a space in memory.
The interpreter determines the storage space in memory based on the type of variable, so you can assign different data types to the variable, such as integer, floating point, string, and so on.
In the previous chapter, we introduced you to the three basic data types of Perl: scalar, array, and hash.
Scalar quantity
$start, such as$a$bis two scalars.Array
@start, such as@a@bis two arrays.Hash
%start%a%bis two hashes.
The variable does not need to explicitly declare the type, and the interpreter automatically allocates the matching type space after the variable is assigned. Variables use the equal sign ( We can use it in the program. The equal sign is a variable on the left and a value on the right. The example is as follows: In the above code, 25, “runoob”, and 1445.50 are assigned values to the variables Next we will see the use of arrays and hashes. Scalar is a single data unit. The data can be integers, floating-point numbers, characters, strings, paragraphs, etc. Simply put, it can be anything. The following is a simple application of scalars: Example The output of the above program execution is as follows: An array is a variable that stores an ordered scalar value. Array To access the variables of an array, you can use the dollar sign ($) + variable name and specify the subscript. The example is as follows: Example The output of the above program execution is as follows: In the program, we are in Hash is a Hash If you want to access the hash, you can use the Example The output of the above program execution is as follows: Context: refers to the location of the expression. The context is determined by the variable type on the left side of the equalsign, the scalar context on the left side of the equal sign, and the list context on the left side of the equal sign. Example The output of the above program execution is as follows: In the code A number of different contexts are listed below:
Perl
separate command space is set 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 differentvariables. 5.6.1. Create variable #
=
) to assign values.
use
strict
statement makes it necessary for all variables to force a type declaration.$age = 25; # integer
$name = "runoob"; # character string
$salary = 1445.50; # floating point number
$age
,
$name
, and
$salary
respectively. 5.6.2. Scalar variable #
#!/usr/bin/perl
$age = 25; # integer
$name = "runoob"; # character string
$salary = 1445.50; # floating point number
print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";
Age = 25
Name = runoob
Salary = 1445.5
5.6.3. Array variable #
@
start.#!/usr/bin/perl
@ages = (25, 30, 40);
@names = ("google", "runoob", "taobao");
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";
$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = google
$names[1] = runoob
$names[2] = taobao
$
the escape character is used before the tag(
\
) so that the characters can be output
$
. 5.6.4. Hash variable #
key/value
that’s right. Gather.
%
start.
$
+
{key}
format to access:#!/usr/bin/perl
%data = ('google', 45, 'runoob', 30, 'taobao', 40);
print "\$data{'google'} = $data{'google'}\n";
print "\$data{'runoob'} = $data{'runoob'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";
$data{'google'} = 45
$data{'runoob'} = 30
$data{'taobao'} = 40
5.6.5. Variable context #
Perl
interpreter determines the type of variable based on the context. Examples are as follows:#!/usr/bin/perl
@names = ('google', 'runoob', 'taobao');
@Copy=@ names# Copy Array
$size=@ names# Assigning an array to a scalar returns the number of array elements
Print "Name is: @ copy n";
Print "The number of names is: $size n";
Name: Google runoob taobao
Number of names: 3
@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 one assigns the array to a scalar, which returns the number of elements of the array.