# Perl 变量

> 原文： [https://beginnersbook.com/2017/02/perl-variables/](https://beginnersbook.com/2017/02/perl-variables/)

perl 中有三种类型的变量：标量，标量和散列数组。让我们在示例的帮助下逐一学习它们。

### 标量

[标量](https://beginnersbook.com/2017/05/scalars-in-perl/)是单一数据单元。标量可以是整数，浮点数，字符串等。标量变量以`$`符号为前缀。让我们看看下面的 perl 脚本，其中我们有三个标量变量。

```perl
#!/usr/bin/perl

# Integer
$age = 29;

# String
$name = "Chaitanya Singh";

# Float
$height = 180.88;

print "Name: $name\n";
print "Age: $age\n";
print "Height: $height\n";
```

**输出：**

```perl
Name: Chaitanya Singh
Age: 29
Height: 180.88
```

### 数组

[数组](https://beginnersbook.com/2017/05/perl-lists-and-arrays/)是标量的有序列表，数组变量的前缀为`@`符号，如下例所示：

```perl
#!/usr/bin/perl

@friends = ("Ajeet", "Leo", "Rahul", "Dhruv");

print "\$friends[0] = $friends[0]\n";
print "\$friends[1] = $friends[1]\n";
print "\$friends[2] = $friends[2]\n";
print "\$friends[3] = $friends[3]\n";
```

**输出：**

```perl
$friends[0] = Ajeet
$friends[1] = Leo
$friends[2] = Rahul
$friends[3] = Dhruv
```

### 哈希（也称为关联数组）

[哈希](https://beginnersbook.com/2017/05/hashes-in-perl/)是一组键值对。哈希变量以`%`符号为前缀。让我们看看下面的例子：

```perl
#!/usr/bin/perl

%age = ('Chaitanya Singh', 29, 'Ajeet', 28, 'Lisa', 25);

print "\$age{'Lisa'}: $age{'Lisa'}\n";
print "\$age{'Chaitanya Singh'}: $age{'Chaitanya Singh'}\n";
print "\$age{'Ajeet'}: $age{'Ajeet'}\n";
```

**输出：**

```perl
$age{'Lisa'}: 25
$age{'Chaitanya Singh'}: 29
$age{'Ajeet'}: 28
```