#!/usr/bin/perl-w

#########################################################################

#FileName:test.pl


#########################################################################

print"helloword\n";

print"----变量定义用\$符号------\n";

$a=`echoxxxx`;

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"----ifelsifelse条件判断语句----\n";

$if1=2;

if($if1==1){

print"pass\n";

}elsif($if1==2){

print"==2\n";

}else{

print"fail\n";

}

#chomp($in1=<STDIN>);

#print"yourinput:$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"poppush针对数组的末尾操作\n";

print"pop取出最后一个,返回数组剩下的,push将一个元素添加到末尾\n";

print"shiftunshift为poppush的反向操作,针对数组头\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(eatingrocksiswrong);

$array5="right";

print"@array5...$array5\n";

print"thisis$array5[3]\n";

print"thisis${array5}[3]\n";

print"thisis$array5"."[3]\n";

print"thisis$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(bedrockslatelava);

print"beforerocks:\n@rocks\n";

foreach$rock(@rocks){

#如果有redo应该跳到的位置

$rock="\t$rock";

$rock.="\n";

print"newrock:$rock\n";

}

print"afterrocks:\n@rocks\n";

print"---------reverse翻转----------\n";

@reverse1=reverse6..9;

print"@reverse1\n";

@reverse2=reverse@reverse1;

print"@reverse2\n";

print"---------sort排序-------------\n";

@sort1=qw(aaaccceeebbbddd);

print"@sort1\n";

@sort1=sort(@sort1);

print"@sort1\n";

print"---------context-----------\n";

print"变量和列表会根据上下文给出不同的值\n";

print1+@sort1."\n";

@rocks=qw(xxxaaaccc);

print"Ihave",@rocks,"rocks!\n";

#强制转换为标量

print"Ihave",scalar@rocks,"rocks!\n";

print"---------sub----------------\n";

subsubtest1{

$temp=$a.$b;

$temp;

}

$a="1";

$b="2zzzzz";

$c=&subtest1;

print"c:$c\n";

subaddfunc{

$size=@_;

print"paramsize:$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";

submax{

my($maxValue)=shift@_;

foreach(@_){

if($_>$maxValue){

$maxValue=$_;

}

}

$maxValue;

}

@numset=(100,600,3000,3001);

$maxValue=&max(@numset);

print"maxValue:$maxValue\n";

subwhich_element_is{

if(@_<2){

return"输入参数个数不能小于2";

}

my($what,@array)=@_;

my$index=0;

foreach(@array){

if($whateq$_){

#判断字符串相等不能使用==,只能用eq

print"Findthiselement[$_]:$index\n";

return$index;

}

$index++;

}

return"can'tfindthiselement";

}

@names=qw(kevinxiangjie);

$which_element=&which_element_is("jie",@names);

print"which_element:$which_element\n";

print"上面的程序都是不严谨语法,开头添加usestrict变为严谨语法检测\n";

print'定义变量时需要用my定义,my$baa=1;'."\n";