Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19746347
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类:

2009-06-04 16:45:36

§2    标量数据

       标量一般为单数。包含数值类型和字符串。

§2.1  数值类型

       Perl把所有数值按双精度浮点数来处理。

       数值举例:

       浮点型

    255.000
    255.0
    7.25e45  # 7.25 times 10 to the 45th power (a big number)
    -6.5e24  # negative 6.5 times 10 to the 24th
             # (a big negative number)
    -12e-24  # negative 12 times 10 to the -24th
             # (a very small negative number)
-1.2E-23 # another way to say that - the E may be uppercase
整型:
    0
    2001
    -40
    255
    61298040283768 也可以这样书写:61_298_040_283_768,逗号有其他用途。
    
    非十进制:
    0377       # 377 octal, same as 255 decimal
    0xff       # FF hex, also 255 decimal
    0b11111111 # also 255 decimal
    转换可以使用oct( ) or hex( ).同样可以使用分隔符:
    0x1377_0B77
0x50_65_72_7C
 
运算:
    2 + 3      # 2 plus 3, or 5
    5.1 - 2.4  # 5.1 minus 2.4, or 2.7
    3 * 12     # 3 times 12 = 36
    14 / 2     # 14 divided by 2, or 7
    10.2 / 0.3 # 10.2 divided by 0.3, or 34
    10 / 3     # always floating-point divide, so 3.3333333...
取模:10.5 % 3.2 is computed as 10 % 3
取幂:**,不适合负数。可以使用Math::Complex来实现。
 
 

§2.2  字符串

       Perl中没有NUL,是用字符数来统计的。

       单引号字符串:

       除了单引号和反斜线之外,都是代表自己。

 'fred' # those four characters: f, r, e, and d

'barney' # those six characters

'' # the null string (no characters)

'Don\'t let an apostrophe end this string prematurely!'

'the last character of this string is a backslash: \\'

'hello\n' # hello followed by backslash followed by n

'hello

there' # hello, newline, there (11 characters total)

'\'\\' # single quote followed by backslash

       \n代表\n, 不代表转义。

 

       双引号可以代表转义,有变量替换。

"barney" # just the same as 'barney'

"hello world\n" # hello world, and a newline

"The last character of this string is a quote mark: \""

"coke\tsprite" # coke, a tab, and sprite

       转义字符表见教材。

      

       字符串操作符

       点号可以连接字符串。"hello" . "world" # same as "helloworld"

       乘号可以复制:"hello" . "world" # same as "helloworld"。数值类型先转换为整数再复制。

       数值和字符的自动转换:由操作符决定,比如;"12" * "3" 36"12fred34" * " 3"也是如此。非数值转为数值时值为0. "Z" . 5 * 7 # same as "Z" . 35, or "Z35"

 

§2.3 perl内置告警

使用方法:

$ perl -w my_program

#!/usr/bin/perl –w

#!perl –w

5.6以后可以使用:use warnings;

       使用后:Argument "12fred34" isn't numeric

       还可以使用诊断:use diagnostics;2个东东都可参考perldiag的手册页。

       另外一种方法:$ perl -Mdiagnostics ./my_program

 

§2.4  标量变量

       区分大小写的。使用的时候都要加上$

       另有: +=,.=**=等运算符。

 

§2.5  print输出

 

$meal = "brontosaurus steak";

$barney = "fred ate a $meal"; # $barney is now "fred ate a brontosaurus steak"

$barney = 'fred ate a ' . $meal; # another way to write that

 

       输出时可以不加引号:print $fred;

         变量分割;

$what = "brontosaurus steak";

$n = 3;

print "fred ate $n $whats.\n"; # not the steaks, but the value of $whats

print "fred ate $n ${what}s.\n"; # now uses $what

print "fred ate $n $what" . "s.\n"; # another way to do it

print 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way

       运算符的优先级参见教材31页。比较操作符见教材33页。

 

§2.6  if控制结构

       Perl没有专门的布尔类型,数值0表示false,其他表示true。字符串’’表示false。其他类型转换为数字或者字符串。字符串'0'也表示false

 

§2.7 获取用户输入

       输入的结束会有回车。CPANTerm::ReadLine可以更好的控制输入。

$line = ;

if ($line eq "\n") {

print "That was just a blank line!\n";

} else {

print "That line of input was: $line";

}

       可以使用chomp去掉回车。

$text = "a line of text\n"; # Or the same thing from

chomp($text); # Gets rid of the newline character

 

chomp($text = ); # Read the text, without the newline character

$text = ; # Do the same thing...

chomp($text); # ...but in two steps

       有无括号均可,只去除一个换行符号。

       变量初始具有undef值,针对数值类型为0。针对字符串为空串。

$madonna = ;

if ( defined($madonna) ) {

print "The input was $madonna";

} else {

print "No input available!\n";

}

阅读(2766) | 评论(0) | 转发(0) |
0

上一篇:休息与锻炼

下一篇:自动化测试介绍

给主人留下些什么吧!~~