Latest web development tutorials

Perl variables

Variable data is stored in memory, a variable that is created will open up a space on the memory.

The interpreter will be determined according to the type of variable memory space in memory, so you can assign different data types, such as integer, floating point, string, and other variables.

In the previous chapter we have to introduce the Perl three basic data types: scalars, arrays, hashes.

    $ Scalar starts as $ a $ b are two scalar.
  • @ Array start, such as @a @b is two arrays.
  • Start hash%,% a% b is two hashes.

Perl variable for each type of command set up an independent space, so different types of variables can use the same name, you do not have to worry about conflict. For example, $ foo and @foo are two different variables.

Create variables

Variables do not need to explicitly declare the type, after variable assignment, the interpreter will be automatically assigned to match the type of space.

Variable use the equal sign (=) to assign.

We can useuse strict statement to declare all variables need to force the type of the program.

The left hand side is a variable, the right value, examples are as follows:

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

Above code 25, "w3big" and 1445.50 respectively assigned to the$age, $ name and$ salaryvariable.

Next we will see the use of arrays and hashes.

Scalar variables

Scalar data is a single unit. Data can be integers, floats, characters, strings, paragraphs, and so on. Simple it can be anything. Here is a simple scalar application:


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

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

The above program execution output is:

Age = 25
Name = w3big
Salary = 1445.5

Array variable

An array is a variable used to store an ordered scalar values.

@ Array begins.

To access the array variable, you can use the dollar sign ($) + variable name and specify the index to access, are shown below:


@ages = (25, 30, 40);             
@names = ("google", "w3big", "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 above program execution output is:

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

Before the program we use the $ mark escape character (\), so as to output the character $.

Hash Variables

Hash is akey / value pairs.

Hash% start.

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


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

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

The above program execution output is:

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

Variable context

The so-called Context: refers to the location where the expression.

The context is determined by the type variable left hand side of the equal sign on the left is a scalar, it is a scalar context, the equal sign on the left is a list, it is a list context.

Perl interpreter based on the context to determine the type of the variable. Examples are as follows:


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

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

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

The above program execution output is:

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

@names Code is an array, it is applied in two different contexts. The first copy it to another array, it outputs all the elements of the array. The second will be assigned to a scalar array, which returns the number of elements in the array.

Here are a variety of different contexts:

No. Context and description
1 Scalar -

Assignment to a scalar variable, in the right context scalar computing

2 List -

Assigned to an array or hash, in the context of the right of the list calculated.

3 Istanbul -

Boolean context is a simple expression evaluates to see whether it is true or false.

4 Void -

This relationship does not need to return the context of what values, generally do not need a return value.

5 Interpolation -

This occurs only in the context of quotes.