---
title: "perl(4)-变量内插"
author: "kino"
date: "2008-06-26T00:15:49Z"
tags: ["Blog", "Perl"]
slug: "perl-4"
draft: false
lastmod: "2008-06-26T00:15:49Z"
id: 208055
url: "https://kino.iteye.com/blog/208055"
---

在一个双引号字符串中，如果包含一个变量名，这个部分将会被 替换成该变量的值。这就叫做变量内插。

 

1、一个例子：

$name = kino;

print "My name is $name"; #执行结果是"My name is kino"

 

2、变量内插只对双引号字符串有效，对单引号字符串是无效的。

print 'My name is $name';#$name并无特殊意义

 

3、如果双引号字符串中有一个未定义的变量，将以空值替换

print "Your name is $yourname"#执行结果是"Your name is  "

 

4、识别变量名时，将取那个字符串中最长的有意义的那个：

$what = "people";

print "There are many $whats" #它将$whats作为变量名,而我们没定义这个变量，所以执行结果是"There are many  "。这显然不是我们想要的结果

 

5、可以用{}明确指定变量名：

$what = "people";

print "There are many ${what}s"#执行结果"There are many peoples",这就是我们想要的结果

 

6、当我们想输入$时，而不是作为变量名的一部分，可以用\\转义：

print "There are many \\$whats" #结果就是"There are many $whats"

 

 

 

 

**本文地址：http://kino.iteye.com/blog/208055，转载请注明，谢谢！**