perl学习笔记---标量

1.perl 输出时,使用 逗号,连接多个字符串

如:print “The answer is ”,6*7, “.\n”

2.当一个字符串由双引号括起来时,如果变量前没有反斜线,则变量会被其值内插

$mean = “brontosaurus steak”;

$barney = “fred ate a $meal”; #$barney 现在是“fred ate a brontosaurus steak”

$barney = ‘fred ate a’. $meal; #同上

变量内插通常也叫做双引号内插,因为它在双

引号中(而非单引号)才有效

3.如果一个变量未被赋值,这是一种特殊的未定义值,undef

4.如果值为数字,0 是false;其余为真

● 如果值为字符串,则空串(‘’)为false;其余为真

● 如果值的类型既不是数字又不是字符串,则将其转换为数字或字符串后再利用上述规则◆。

◆这意味着undef(很快会看到)为false。所有的引用(在Alpaca 书中有详细讨论)都是true。

5.chomp 操作

chomp ($text = <STDIN>); #读入,但不含换行符

如果结尾有两个或两个以上的换行符◆,chomp 仅去掉一个。如果没有,那什么也不做,返回0。chomp 是一个函数。作为一个函数,它有一个返回值,为移除的字符的个数。这个数字基本上没什么用

5.要分辨其是undef 还是空串,可以使用defined 函数,它将在为undef 时返回false,其余返回true。

use diagnostics

$ perl–Mdiagnostics ./my_program

Argument “12fred34”isn’t numeric in addition(+) at ./m_program line 17 (#1)

(Wnumeric) The indicated string was fed as an argument to

an operator that expected a numeric value instead. If you’re

fortunate the message will identify which operator was so unfortunate