您的位置:首页 > 编程语言 > Java开发

perl基本语言语法(与java,c#不同的地方积累)

2013-10-18 11:58 666 查看
连接字符串 

perl 用 .    “hello”+"hello"   可用x号  "hello"*3  “hellohellohello” 

java,c#用+ "hello"+"hello"

运算符

perl



等待输入:

$line=<STDIN>;

或者

$line=<>;

未定义的字符值

undef --不会报错

当作数字使用时为0

当作字符串使用时为空

判断是否为空 用 defined()

数组的最后一个元素索引 

例如:$list[99]="end";

$#list或者-1为最后一个元素的索引 

也就是说  $list[$#list] 或者 $list[-1]里也是 "end"

清空数组的方法: $list=()

列表

qw -----给列表中的内容加上双引号 例子如下:

qw(a b c)  等于 ("a"  "b"  "c")  

@list=qw(a b c)

从列表中移出或者打入数据

pop push

递增符号..

例如:  6..10  表示6,7,8,9,10

reverse返序输出

reverse 6..10  表示 10,9,8,7,6

子程序定义

sub add

{

$n+=1;

print "This is a number $n";

}

调用子程序(呼叫子程序)

&add;

ps:如果程序的定义在  呼叫之前 可省略与号&

带参数的子程序

$n=&add(1,2);

sub add

{

$_[0]+$_[1];

}

或者定义私有变量

sub add

{

my($m,$n);     #用my定义语块中的私有变量

($m,$n)=@_;    #将参数赋给参数

$m+$n;

}

其他用法:

shift  @_   #得到第一个参数

foreach(@_)

{

$all+=$_;

}

输入输出定向

<    表示此文件只用来输入

> 表示存为一个新文件

>> 表示追加存入文件

异常输出

if(!open LOG,  ">>filename")

{

die "can't open the file:$!"

}

if模块

if()

{}

elsif()

{}

else

{}

跳出循环

perl: 用last

java,c#用 break

next

跳到内循环的底端 然后正常进入下一个循环的判断条件

redo

跳到内循环的顶端,不进入判断条件 直接重做一遍

智能匹配符~~

无论是什么类型的变量(哈希表,字符串,数值) 都能判断相等

条件语句块

perl

given($a)

{

when(/joe/i){say "Name has joe in it'}

when(/^joe/){say 'Name start with joe'}

when('joe') {say 'Name is joe'}

default {say 'i don't see a joe''}

}

java c#

switch(i)

{

   case 1:

    printf("1\n");

    break;

   case 2:

    printf("2\n");

    break;

}

异常处理模块

perl

eval{};

print "An error occurred: $@"  if  $@;

java c#:

try{}

catch{}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: