perl小程序(一)
1请用perl在屏幕输出hello,world# cat perlhello.pl
#!/usr/bin/perl
print "hello,world!\n";
# ./perlhello.pl
hello,world!
2截取出正则的匹配内容,在shell中,真是头都大了
#!/usr/bin/perl -w
$_="Counter-Strike Global Ofensive";
if(/(.*)/)
{
print "$1\n";
}
# ./perlre.pl
Counter-Strike Global Ofensive
稍微改动下,截取括号的内容,也是很简单
#!/usr/bin/perl -w
#注意每一句以分号结尾,注意是否转义
$_="(Counter-Strike Global Ofensive)";
if(/\((.*)\)/)
{
print "$1\n";
}
3接受标准输入,并输出
#!/usr/bin/perl -w
while ()
{print;}
# ./receive.pl
hello,i am liuliancao
hello,i am liuliancao
4 计算圆的面积
#!/usr/bin/perl -w
print "please input the radius of the circle";
$r=;
if($r lt 0)
{$c=0;}
else
{$c=2*$r*3.141592654;}
print "the c is $c\n";
# ./circle.pl
please input the radius of the circle:1
the c is 6.283185308
5 计算两个数的乘积,并输出它们
#!/usr/bin/perl -w
print "please input two number divided by Enter,and i will mutiply them\n";
chomp($a=);
$b=;
print "a is $a, and b is $b andthe muti result is ",$a*$b," !\n";
# ./muti.pl
please input two number divided by Enter,and i will mutiply them
2
5
a is 2, and b is 5
andthe muti result is 10 !
6 重复输出指定次数字符串
#!/usr/bin/perl -w
print "please input string and times divided by Enter!\n";
chomp ($a=);
$b=;
$result=$a x $b;
print "result is $result\n";
# ./stringtimes.pl
please input string and times divided by Enter!
liuliancao
5
result is liuliancaoliuliancaoliuliancaoliuliancaoliuliancao
页:
[1]