工学1号馆

home

perl标量数据

Wu Yudong    December 11, 2016     Linux/Unix   521   

perl使用标量来称呼单个事物,标量是perl中最简单的一种数据类型,标量大部分要么是数字要么是由字符组成的序列

数字

所有的数字内部格式相同,在perl内部总是按照双精度浮点数的要求保存数字并进行运算的,也即是perl内部不含整数值,程序中用到的整数常量会被转换成等效的浮点数值

浮点数直接量:小数点与前置的正负号都是可选的,数字后面可以加上’e’或’E’表示10的幂

perl 允许你在整数直接量中插入下划线,将若干位数分开,例如:123_456_789

八进制数以0开头,十六进制数以0x开头,二进制数以0b开头

接着写一个小程序实践一下:

#! /usr/bin/perl
printf "%f\n", -1.25;
printf "%f\n", 1.25e20;
printf "%f\n", 1.25e8;
printf "%f\n", -1.25e-3;
printf "%f\n", -1.25E2;
printf "%d\n", 123_322_341_6778;
printf "------------------\n";
printf "%d\n", 0377;
printf "%d\n", 0xff;
printf "%d\n", 0b11111111;
printf "------------------\n";
printf "%d\n", 10.2%3.1;
printf "%f\n", 10.2%3.1;
printf "%d\n", 10%3;
printf "%d\n", 3**2;

编译运行:

wu@ubuntu:~/perl$ chmod a+x num.pl
wu@ubuntu:~/perl$ ./num.pl
-1.250000
125000000000000000000.000000
-125000000.000000
-0.001250
-125.000000
1233223416778
——————
255
255
255
——————
1
1.000000
1
9

字符串操作

字符串可以使用. 操作符连起来组成一个新的字符串,原来的字符串不变

字符串重复操作符:str x n,将x左边的字符串str重复指定n次

先来一个程序验证一下

#! /usr/bin/perl
$s1 = "hello ";
$s2 = "wuyudong\n";
$s3 = $s1.$s2;
print $s1;
print $s2;
print $s3;
printf "%s\n", 5x4;

运行

wu@ubuntu:~/perl$ ./string.pl
hello wuyudong
hello wuyudong
5555

数字与字符串之间的自动转换

perl会根据需要自动在数字与字符串之间转换,主要取决于中间的操作符,你不必担心数字和字符串之间的差异,只管合理使用操作符,perl来搞定剩下的工作

来个例子:

#! /usr/bin/perl
printf "%d\n", 0377;
print "0377\n";
printf "%s\n","z".5*7x4;

运行

wu@ubuntu:~/perl$ ./num1.pl
255
0377
z35353535

标量变量

变量以$打头,大小写敏感;如果启用utf8你、编译指令,就可以使用非ASCII字符来作为变量名

字符串中的标量变量内插

将字符串内出现的所有标量变量替换成当前的值,如果标量没有被赋值过,就会用空字符串来替换

当进行内插时,perl会尽可能使用最长且合法的变量名称,有可能会引起歧义,举个例子:

#! /usr/bin/perl
print "the answer is ",2*4,".\n";
$s1 = "wu ";
$s2 = "yudong";
print "my name is $s1$s2\n";
print "my name is ".$s1.$s2."\n";
print "------------------------\n";
$what = "brontosaurus steak";
$n = 3;
print "fred ate $n $whats.\n";  #这里替换的是$whats,未定义,为null
print "fred ate $n ${what}s.\n";  #比较常用的方法,使用{}
print "fred ate $n $what"."s.\n";  #另一种方法,比较麻烦
print 'fred ate ' . $n .' '. $what."s.\n";  #最麻烦的写法

编译运行

wu@ubuntu:~/perl$ ./string.pl
the answer is 8.
my name is wu yudong
my name is wu yudong
————————
fred ate 3 .
fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.
fred ate 3 brontosaurus steaks.

数值与字符串比较操作符:

比较 相等 不等 小于 大于 小于或等于 大于或等于
数字 == != < > <= >=
字符串 eq ne lt gt le ge

获取用户输入

让perl程序读入输入的值,使用“行输入”操作符<STDIN>,放在程序希望的位置上

#! /usr/bin/perl
print "what's your name?\n";
$name = <STDIN>;
if($name eq "\n") {
    printf "this is just a blank line!\n";
} else {
    printf "your name is: $name!";
}

编译运行

wu@ubuntu:~/perl$ ./string.pl
what’s your name?
wuyudong
your name is: wuyudong

chomp操作符

chomp的作用很单一,就是将单个字符变量的末尾的换行符去掉

undef值

当变量首次赋值前的初始值是undef值(未定义)

当数字用的时候是0,当字符串用的时候是空字符串,但undef既不是数字也不是字符串,而是另一种类型的标量值。举个例子:

#! /usr/bin/perl
$n = 1;
while($n < 100) {
    $sum += $n;   #sum未定义
    $n++;
}
print "the total is $sum.\n";

$string .= "more text\n";  #string未定义
print $string;

编译运行

wu@ubuntu:~/perl$ ./test1.pl
the total is 4950.
more text 

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)