Perl variable

A variable is data stored in memory, and creating a variable opens up a space in memory.

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

In the previous section, we've covered three basic data types for Perl: standard, array, hash.

  • The standard measure starts with, such as $$b is two sequestrations.
  • Arrays start, such as @a @b are two arrays.
  • Hash % starts, and %a %b is two hashes.

Perl has a separate command space for each variable type, so different types of variables can use the same name, so 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 types, and after the variable is assigned, the interpreter automatically allocates matching type space.

The variable uses an equal sign to assign a value.

We can use the use strict statement in our program so that all variables need to force the declaration type.

The equal sign is a variable on the left and a value on the right, as follows:

$age = 25;             # 整型
$name = "youj";      # 字符串
$salary = 1445.50;     # 浮点数

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

Next we'll see the use of arrays and hashes.


The standard variable

The calibration is a single data unit. D ata can be integers, floats, characters, strings, paragraphs, and so on. S imply put, it can be anything. H ere is a simple application of the calibration:

#!/usr/bin/perl

$age = 25;             # 整型
$name = "youj";      # 字符串
$salary = 1445.50;     # 浮点数

print "Age = $age\n";
print "Name = $name\n";
print "Salary = $salary\n";

The output of the above program execution is:

Age = 25
Name = youj
Salary = 1445.5

Array variables

An array is a variable used to store an ordered synth value.

Array s start.

To access the variables of the array, you can use the dollar sign ($) plus variable name and specify a subsem to access them, as shown in the following example:

#!/usr/bin/perl

@ages = (25, 30, 40);             
@names = ("google", "youj", "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";

The output of the above program execution is:

$ages[0] = 25
$ages[1] = 30
$ages[2] = 40
$names[0] = google
$names[1] = youj
$names[2] = taobao

In the program, we used the escape character before the tag, so that the character $can be output.


Hash variable

The hash is a collection of key/value pairs.

Hash % starts.

If you want to access the hash value, you can access it in the format of $ .

#!/usr/bin/perl

%data = ('google', 45, 'youj', 30, 'taobao', 40);

print "\$data{'google'} = $data{'google'}\n";
print "\$data{'youj'} = $data{'youj'}\n";
print "\$data{'taobao'} = $data{'taobao'}\n";

The output of the above program execution is:

$data{'google'} = 45
$data{'youj'} = 30
$data{'taobao'} = 40

The variable context

The so-called context: refers to where the expression is located.

The context is determined by the type of variable to the left of the equal sign, the calibration to the left, the severity context to the left, the list to the left, and the list context to the left.

The Perl interpreter determines the type of variable based on the context. Here's an example:

#!/usr/bin/perl

@names = ('google', 'youj', 'taobao');

@copy = @names;   # 复制数组
$size = @names;   # 数组赋值给标量,返回数组元素个数

print "名字为 : @copy\n";
print "名字数为 : $size\n";

The output of the above program execution is:

名字为 : google youj taobao
名字数为 : 3

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

A number of different contexts are listed below:

Serial number Context and description
1 The standard . .

The assignment is given to a scalc variable, which is calculated to the right of the standard context

2 List . .

The assignment is given to an array or hash, which is calculated to the right of the list context.

3 Boolean . .

Boolean context is a simple expression calculation to see if it is true or false.

4 Void −

This context does not require the relationship to return any value, and generally does not need to return a value.

5 Interpolation . .

This context occurs only in quotation marks.