微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Perl入门一

以下在ubuntu11.10上运行


1.在用户lord( 本人)下新建temp文件

输入代码

$celsius = 20;
while($celsius <=45)
{
$fahrenheit =($celsius *9/5)+32;
print "$celsius C is $fahrenheit F.\n";
$celsius =$celsius +5;
}
在Terminal中输入命令:perl  temp

slave1@slave1-data:~$ perl -w temp
20 C is 68 F.
25 C is 77 F.
30 C is 86 F.
35 C is 95 F.
40 C is 104 F.
45 C is 113 F.

2.一个正则表达式用法

print "Enter a temperature in Celsius:\n";
$celsius =<STDIN>;#从控制台输入
chomp($celsius);#去掉换行符
if($celsius =~ m/^[0-9]+$/){
$fahrenheit =($celsius *9/5)+32;
print "$celsius C is $fahrenheit F.\n";
}
else{
print "Exception: input a number\n";
}

『=~』 是“匹配”的意思

『m』 是match

/。。。/中是正则表达式

3.for语句

#! /usr/bin/perl

for $x (1..50)

{

 print "The value of \$x is $x\n";

};

$x要在()外边,与C风格不同,相当于for(int i=1;i<50;i++){}

the '\' tells perl totreat the next character as it looks.

4.while(){}和until(){}语句

$x = 0;
while($x <= 50){
 print "The value of \$x is $x\n";
 $x++;
}
#! /usr/bin/perl

$x = 0;
until ($x > 50)
{
 print "The value of \$x is $x\n";
 $x++;
}

5.对文件的操作

#!/usr/bin/perl

open(myfile,"G:\\dataOfJava\\template\\admin-templates-1\\admin_template\\index.html");
while(<myfile>){
	print;
	};
close(myfile);

注意:1.文件路径不能有中文

            2.『\\』只针对于Windows下的文件,在Linux中用"/usr/bin/..."之类的


6.写入文件

#! /usr/bin/perl

open (FILE,">myfile.txt");
print FILE "This is a test of something to be in this file";
close FILE;
> 提示输入哪个文件

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐