以下在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;
> 提示输入哪个文件