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

关于perl

发布时间:2020-12-16 00:27:33 所属栏目:大数据 来源:网络整理
导读:一、????????Perl中常见的函数 1.????chop函数会砍掉字符串变量的最后一个字符,并返回砍掉的字符。示例:$last_char = chop($var); 2.????split函数?split(/pattern/,$text,limit)其中/pattern/是文字处理模式,而limit是代表要分割的个数,一般可以省略。

一、????????Perl中常见的函数

1.????chop函数会砍掉字符串变量的最后一个字符,并返回砍掉的字符。示例:$last_char = chop($var);

2.????split函数?split(/pattern/,$text,limit)其中/pattern/是文字处理模式,而limit是代表要分割的个数,一般可以省略。

3.????keys函数?keys(%array) 取出关联数组%array中全部的key.

4.????Values函数 values(%array) 取出关联数组%array中全部的value.

5.????reverse函数 reverse(@array)将数组中的元素由后到前重新排列。

6.????sort函数 sort(@array)将数组中的元素由小到大排列

7.????length函数 length($string)求字符串中$string的字节值

8.????substr函数 substr($string,offset,length)返回给定字串的子串。Offset代表起始字符的位置,length代表引用的字符串的长度。

9.????index函数 index($string,$substring,position)在字串中寻找子串。

10.? push函数 push(@array,$string)在数组中添加新的元素到数组的末尾

11.? pop函数 pop(@array)将数组的最后的一个元素删除,并将删除的元素返回

12.? unshift函数在数组的第一个元素前附加新的元素。

13.? shift函数将数组的第一个元素删除,并返回

14.? join函数 join($string,@array) 在数组@array的元素之间加上一些指定的字符$string,并将结果返回。

15.? grep函数 grep(/pattern/,@array)将符合文字处理模式的数组元素找出来。

16.? rand函数 rand($interger)常和srand配合来取随机数。

二、????????在perl中连接数据库

??

sub? connect_mysql

{

my?? %info =(dbname=>$CCMS_GENERAL{'database'}{'dbname'},

??????????? ??dbhost=>$CCMS_GENERAL{'database'}{'dbhost'},

?????????????dbport=>$CCMS_GENERAL{'database'}{'dbport'},

???????????? ?dbuser=>$CCMS_GENERAL{'database'}{'dbuser'},

?????????????dbpasswd=>$CCMS_GENERAL{'database'}{'dbpasswd'},

?????????????dbsock=>$CCMS_GENERAL{'database'}{'dbsock'});

$dbh=DBI->connect("DBI:mysql:database=$info{'dbname'};host=$info{'dbhost'};port=$info{'dbport'};mysql_socket=$info{'dbsock'}",$info{'dbuser'},$info{'dbpasswd'})ordie "Can't Connect Database Server: $!";

return($dbh);

}

三、????????在perl中连接AMI

sub connect_ami

{

my??? ($res,$ret1);

my??? %info = (host=>'localhost',

?????????????? port=>5038,

?????????????? user=>'ccms',

?????????????? secret=>'ccms'

????????????? );

#CONNECT

my??? ($host,$addr,$msg);

????? $host = inet_aton($info{'host'});

?????socket($SOCK,AF_INET,SOCK_STREAM,getprotobyname('tcp'));

????? $addr = sockaddr_in($info{'port'},$host);

????? connect($SOCK,$addr) or die "Can'tConnect to Asterisk Manager Port : $!";

????? $msg = <$SOCK>;

????? if ($msg !~ /Asterisk Call Manager/){

???????????? die "Connect not ok!";

???????????? exit;

????? }

????? send($SOCK,"ACTION:LOGINrnUSERNAME: $info{'user'}rnSECRET: $info{'secret'}rnEVENT:ONrnrn",0);

????? $res = <$SOCK>;

????? if ($res =~ /Success/){

????????? $ret1 = "a";

?????}else{

????????? $ret1 = "b";

????? }

return($ret1);

}

四、????????多进程

use Socket;

use DBI;

use strict;

use warnings;

use POSIX ":sys_wait_h";

my?$num_proc=0;

my?$num_collect=0;

my?$zombies;

my?$collect;

$SIG{CHLD}=sub{$num_proc--};

for (my $i = 0; $i < 10; $i ++){ ? ? ## == fork a new process == ? ? my $pid = fork(); ? ? if (!defined($pid)) { ? ?? ???print "Error in fork: $!"; ? ?? ???exit 1; ? ? } ? ? if ($pid == 0) { ? ?? ???## == child proc == ? ?? ???print "Child $i : My pid =$$n"; ? ?? ???sleep(5); ? ?? ???print "Child $i : endn"; ? ?? ???exit 0; ? ? } ? ? ## == if need to collect zombies == ? ? if ($zombies > 0) { ? ?? ???while (($collect = waitpid(-1,WNOHANG))> 0) { ? ?? ?? ?? ?$zombies --; ? ?? ???} ? ? } ? ? sleep(1); }

(编辑:李大同)

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

    推荐文章
      热点阅读