perl学习笔记-1

#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 = <STDIN>;
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";

猜你喜欢

转载自www.cnblogs.com/smallfishy/p/13171411.html