加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 00:07:03 所属栏目:大数据 来源:网络整理
导读:连接字符串? perl 用 . ? ?“hello”+"hello" ? 可用x号 ?"hello"*3 ?“hellohellohello” java,c#用+ "hello"+"hello" 运算符 perl 等待输入: $line=STDIN; 或者 $line=; 未定义的字符值 undef --不会报错 当作数字使用时为0 当作字符串使用时为空 判断是否


连接字符串?

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("1n");
? ? break;
? ?case 2:
? ? printf("2n");
? ? break;
}


异常处理模块

perl

eval{};

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


java c#:

try{}

catch{}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读