#Example1 #while循环
#!/usr/bin/perl
use 2.010; #声明本校本所使用的版本号
while(<>){ #接收键盘的任意输入
chomp;
print join("\t",(split /:/)), "\n"; 首先用':'分隔字符串,然后用'\t'(制表符)连接
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example2 #函数
#!/usr/bin/perl
@lines = `perldoc -u -f atan2`;
foreach (@lines) {
s/\w<([^>]+)>/\U$1/g;
print;
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example3 #真正的第一个可以运行的东东,只输出一句话
#!/usr/bin/perl
printf ("%s","hello"." "."word\n"); #格式控制(%s)只对printf有效
print ("%s","hello"." "."word\n"); #格式控制(%s)被当做字符输出
print "hello\ word\!\n"; #括号可以删除掉的
print "hello word!\n"; #空格和叹号的转义也可以删掉
print ("%s","hello"," ","word\n"); #下面四个的输出完全一样
print ("%s"."hello"." "."word\n");
print "%s","hello"," ","word\n";
print "%s"."hello"." "."word\n";
say "%s"."hello"." "."word\n"; #这个也是输出一句话,不过要求版本最低是5.010
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example4 #接收输入参数
#!/usr/bin/perl
$line =
if ($line eq "\n"){ #如果只有一个回车
print "That was just a blank line!\n";
} else {
print "That line of input was: $line";
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example5 #见识一下perl里的赋值操作
#!/usr/bin/perl
print "5" x 4; #输出4个5
print "\n"; #输出一个回车
print "yu_qq" x (4+1); #输出( 4+1)=5遍的yu_qq
print "\n";
$lin = "abc" ;
$ll = "f";
$lin .= "d"; #'.'连接字符串,和JAVA中的'+'相同,把d加到$lin(abc)后面
$lin = $ll . $lin; #把两个($ll和$lin)连在一块儿
print "$lin";
print "\n";
$num= "5" x 4;
print "$num\n";
$num -= 5500; #双目操作运算,连在一起简写了
print "$num\n";
$num *= $num;
print "$num\n";
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example6 #数组(i堆栈)
#!/usr/bin/perl
@arry = qw/ a1 a2 b3 c4 d5 d6/; #声明一个堆栈
print "@arry" . "\n";
@ff = pop(@arry); #出栈(最后一个d6)
print "@ff" . "\n";
print "@arry" . "\n";
push(@arry, b6,d7); #入栈把b6和d7压进去(在结尾)
print "@arry" . "\n";
@ff = shift(@arry); #出栈(第一个a1)
print "@ff" . "\n"; #
print "@arry" . "\n";
unshift(@arry, d1,c1); #入栈把d1和c1压进去(在开头)
print "@arry" . "\n";
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example7 #foreach 循环
#!/usr/bin/perl
foreach (1..10) {
print "I can count $_!\n"; # $_ perl内置变量
}
foreach $rack (qw/ 1 2 3 10/) { #也可以指定变量$rack,循环控制参数qw/ 1 2 3 10/
print "I can count $rack!\n";
}
@rack= qw/ 110 11 we esfd ef/; #还可以使用数组@rack
foreach $rack (@rack) {
print "I can count $rack!\n";
}
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example8 #标量与列表及其上下环境
#!/usr/bin/perl
@rock = qw( tals dfkl dj yu sj ); #某种程度上可以简单理解为@是列表(数组、堆栈...),$是标量(变量、字符串、数字、Boolean...)
$rock = @rock; #取@rock列表长度
print "@rock" . "\n"; #输出列表@rock
print @rock; #去除中间空格输出@rock
print @rock . "\n"; #输出@rock的长度
print $rock . "\n"; #输出@rock的长度
print "$rock" . "\n"; #输出@rock的长度
print scalar @rock; #输出@rock的长度
@ar = ("1542","123","0452"); #这种写法和qw//一样的
$ar = scalar @ar; #取@ar列表长度
print "@ar\n"; #输出列表@ar
$ar += 67; #对$ar进行加法操作
print "$ar\n"; #输出$ar的值
@ar = sort @ar; #对@ar进行排序(不是按照数值大小,而是按照ASCII码排序)
print "@ar\n"; #输出列表@ar
print @ar; #
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example9 #子程序的调用
#!/usr/bin/perl -w #输出警告信息
sub sum { #定义子程序sum,该程序将会被覆盖
print "I'm sub_sum!\n"; #表明身份
$ff + $dd; #将计算$ff和$dd的和作为子程序的返回
}
$ff = 3;
$dd = 5;
$aa = ∑ #调用子程序sum,并将其返回值赋值给$aa
print "\$SUM is $aa.\n"; #输出结果
print "\$ff is $ff.\n";
sub sum { #重新定义子程序sum,该子程序将覆盖上面的sum
$ff = $ff + $dd; #计算$ff和$dd的和,赋值给$ff
$ff + $dd; #计算$ff和$dd的和,这个语句不是最后一条,
#执行结果不会作为子程序的返回,计算结果会被抛弃
print "I'm sub_sum!\n"; #print 语句执行的结果作为返回值(如果没有后面这句)
$ff = $ff + $dd; #将$ff和$dd的和相加后赋值给$ff,并将后来的$ff作为返回值
}
$aa = 3 * ∑ #调用子程序sum #调用子程序sum,并将其返回值赋值给$aa
print "\$SUM is $aa.\n"; #输出结果
print "\$ff is $ff.\n";
#-----------------------------------------------------------------------------------------------------------------------------------------------------
#Example10 #哈希数据
#!/usr/bin/perl
$family_name{"fred"} = "yu";
$family_name{"yu"} = "QQ";
$family_name{"QQ"} = "yu";
foreach $person (qw< fred yu QQ >) {
print "I've heard of person $family_name{$person}.\n";
}
$foo = "Q";
print $family_name{ $foo . "Q" }."\n";