Perl基本语法实例,变量,数组,函数,if,for,while-创新互联
#!/usr/bin/perl -w
公司主营业务:成都做网站、网站制作、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。创新互联是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。创新互联推出黄州免费做网站回馈大家。#########################################################################
# File Name: test.pl
#########################################################################
print "hello word \n";
print "---- 变量定义用\$ 符号------\n";
$a=`echo xxxx`;
print "$a";
$b='aaaa'.'hhhh';
$c="bbbb";
$d="$b"."$c";
$d.='eeee';
print "$d\n";
print "d: $d\n";
$bl1 = 1>=0;
$bl2 = 1>=3;
print $bl1;
print $bl2;
print "---- if elsif else 条件判断语句 ----\n";
$if1=2;
if($if1 == 1){
print "pass\n";
}elsif($if1 == 2){
print "==2\n";
}else{
print "fail\n";
}
#chomp($in1 =
); #print "your input: $in1\n";
#if(defined($in2)){
# print "1";
#}else{
# print "2";
#}
print " ------- \@ 符号来定义数组 ------ \n";
@array = (1,2,3,"4")x4;
print "$array[1]\n";
print "$#array\n";
print "$array[-1] \n";
$xtest="123"x5;
print "$xtest\n";
print "pop push 针对数组的末尾操作 \n";
print "pop 取出最后一个,返回数组剩下的, push将一个元素添加到末尾 \n";
print "shift unshift 为 pop push的反向操作,针对数组头\n";
@array2 = 5..10;
print "$#array2 ---- $array2[-1] \n";
pop @array2;
print "$#array2 ---- $array2[-1] \n";
pop(@array2);
print "$#array2 ---- $array2[-1] \n";
print "---------- array3 ----------\n";
@array3 = 1..3;
print "$#array3 ---- $array3[-1] \n";
push @array3,9;
print "$#array3 ---- $array3[-1] \n";
push @array3,10...12;
print "$#array3 ---- $array3[-1] \n";
print "---------- array4 ----------\n";
@array4 = 1..3;
print "@array4 ---- $array4[0] \n";
shift @array4;
print "@array4 ---- $array4[0] \n";
unshift @array4,99;
print "@array4 ---- $array4[0] \n";
unshift(@array4,'98');
print "@array4 ---- $array4[0] \n";
print "---------- array5 ----------\n";
@array5 = qw(eating rocks is wrong);
$array5 = "right";
print "@array5 ... $array5 \n";
print "this is $array5[3] \n";
print "this is ${array5}[3]\n";
print "this is $array5"."[3]\n";
print "this is $array5\[3] \n";
print "--------- for --------------\n";
for($i=0; $i<10; $i++){
print "for: $i\n";
}
#实际for和foreach是一样的
#中断循环对比C 的break和continue,Perl分别对应的是 last 和 next
#Perl中还特有 redo, 调到当前循环的顶端,不进行条件判断就再次执行本次循环
#next会进入下一次循环,redo会继续执行本次循环
print "--------- foreach ----------\n";
@rocks = qw(bedrock slate lava);
print "before rocks: \n @rocks \n";
foreach $rock (@rocks){
#如果有 redo 应该跳到的位置
$rock = "\t$rock";
$rock .= "\n";
print "new rock: $rock \n";
}
print "after rocks: \n @rocks \n";
print "--------- reverse 翻转 ----------\n";
@reverse1 = reverse 6..9;
print "@reverse1 \n";
@reverse2 = reverse @reverse1;
print "@reverse2\n";
print "--------- sort 排序 -------------\n";
@sort1 = qw(aaa ccc eee bbb ddd);
print "@sort1\n";
@sort1 = sort(@sort1);
print "@sort1\n";
print "--------- context -----------\n";
print "变量和列表会根据上下文给出不同的值\n";
print 1+@sort1."\n";
@rocks = qw(xxx aaa ccc);
print "I have",@rocks,"rocks!\n";
#强制转换为标量
print "I have", scalar @rocks, "rocks!\n";
print "--------- sub ----------------\n";
sub subtest1{
$temp=$a.$b;
$temp;
}
$a = "1";
$b = "2zzzzz";
$c = &subtest1;
print "c: $c\n";
sub addfunc{
$size = @_;
print "param size: $size\n";
my($num1, $num2);
($num1, $num2) = @_;
$num1 + $num2;
}
$addresult = &addfunc(100,99);
print "addresult: $addresult\n";
print "-------- 子程序 == 函数 ----\n";
print "函数参数放在 \@_数组内,可以用shift循环的切出第一个参数\n";
print "my() 定义局部变量\n";
print "子程序的返回值就是最后一部的计算值\n";
sub max{
my($maxValue) = shift @_;
foreach(@_){
if($_>$maxValue){
$maxValue = $_;
}
}
$maxValue;
}
@numset = (100,600,3000,3001);
$maxValue = &max(@numset);
print "maxValue: $maxValue\n";
sub which_element_is{
if(@_ < 2){
return "输入参数个数不能小于2";
}
my($what, @array) = @_;
my $index = 0;
foreach(@array){
if($what eq $_){
#判断字符串相等不能使用==,只能用eq
print "Find this element [$_]: $index\n";
return $index;
}
$index++;
}
return "can't find this element";
}
@names = qw(kevin xiang jie);
$which_element = &which_element_is("jie", @names);
print "which_element: $which_element\n";
print "上面的程序都是不严谨语法, 开头添加 use strict 变为严谨语法检测\n";
print '定义变量时需要用my定义, my $baa = 1; '."\n";
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章名称:Perl基本语法实例,变量,数组,函数,if,for,while-创新互联
文章源于:http://azwzsj.com/article/diiicd.html