- $line = <STDIN>;
- if ($line eq "\n") {
- print "That was just a blank line!\n";
- } else {
- print "That line of input was: $line";
- }
- $text = "a line of text\n";
- chomp($text)
- (1,2,3) //包含1、2、3这三个数字的列表
- ("fred",4.5) //两个元素,"fred"和 4.5
- (1..100) //100个整数构成的列表
- (1..5) //与(1,2,3,4,5)相同
- (1.7..5.7) //同上
- (5..1) //空列表 仅向上计数
- (0,2..6,10,12) //与(0,2,3,4,5,6,10,12)相同
- ……
- qw! fred barney betty wilma dino !
- qw{ fred barney betty wilma dino }
- 这两个例子是以!和{}来做为定界符
- perl还允许你用其他任何标点符号作为定界符
- ($fred,$barney,$dino) = (“flintstone”,”rubble”,undef);
- ($fred,$barney) = qw< flintstone rubble slate granite>;
- push
- @array = 5..9;
- $fred = pop(@array); # $fred 变成9 @array现在是(5,6,7,8)
- $barney = pop @array; # $barney 变成8 @array现在是(5,6,7)
- pop @array; # @array现在是(5,6)
- push(@array,0); # @array现在是(5,6,0)
- push @array,8; # @array现在是(5,6,0,8)
- push @array,1..10; # @array 得到了10个新元素
- @others = qw/ 9 0 2 1 0/;
- push @array, @others; # @array 又得到了5个新元素
- @array = qw# dino fred barney #;
- $m = shift(@array); //$m 变成 “dino”, @array 现在是 (“fred”,”barney”)
- $n = shift @array; //$n 变成 “fred”, @ array 现在是 (”barney”)
- shift @array; //@ array 现在变空了
- $o = shift @array; //$o 变成undef,@array还是空的
- unshift (@array, 5); //@array 现在仅包含一个元素的列表(5)
- unshift @array, 4; //@array 现在是(4,5)
- @others = 1..3;
- unshift @array, @others; //@array 变成了 (1,2,3,4,5)
- foreach $rock (qw/ bedrock slate lava /) {
- print “one rock is $rock.\n”; //依次打印三种石头的名字
- }
- foreach (1..10) { #默认会用 $_ 迭代
- pirnt “I can count to $_!\n”;
- }
- $_ = “Yabba dabba doo\n”;
- print; # 默认打印 $_