perl 语言(变量类型)

perl之变量类型

perl 语言将变量类型分为标量,数组和哈希,perl在定义变量时会先加入一些符号来代表他们的类型。标量用$, 数组用@,哈希用%

#!/usr/bin/perl
$age = 25;
# 25
print "$age";
# $age = 25
print "/$age = $age\n";
@animals = ("Cat","Dog","fish","rice");
# fish
print "@animals[2]";
%gender = ('xiaohong','female','xiaolan','male');
# female
print "gender{'xiaohong'}";

标量可以是字符,整型,浮点型;哈希可以理解为Python里的字典。

gender = {'xiaohong':'female','xiaolan':'male'}
# female
print gender['xiaohong']
name = ['xiaohong','xiaolan']
gender = ['female','male']
student = dict(zip(name,gender))
# {'xiaohong':'female','xiaolan':'male'}
print student

变量上下文

上下文是由等号左边来决定的,等号左边是标量,那么标量就是上下文。

其中, 数组转标量,得到的是数组的个数,而标量转数组还是标量的值。而两个相同类型的变量互相转化则是起到了复制的功能。

#!/usr/bin/perl
@animals = ('dog','cat','rice','butterfly');
$name = 25;
@name = $name;
# 25
print "@name\n";
$animals = @animals;
# 4
print "$animals";

双引号和单引号的区别

在perl语言中,如果想要表达转义字符需要用双引号。
常用的转义字符有:

常用转义字符 含义
\\ \
\’
\’’ ‘’
\n 换行
\r 回车
\u or \low 下一个字母大写(小写)
\U or \L 所有字母大写(小写)
\Q 将\E之前的所有非单词字符加上\
$str = "\QHellow world\n";
# Hello\ world\
print "$str\n";

参考菜鸟教程

发布了6 篇原创文章 · 获赞 0 · 访问量 64

猜你喜欢

转载自blog.csdn.net/weixin_44935881/article/details/105692536
今日推荐