perl 函数集(一)
一、进程处理函数
1、进程启动函数
2、进程终止函数
3、进程控制函数
4、其它控制函数
二、数学函数
三、字符串处理函数
1、进程启动函数
函数名 | eval |
调用语法 | eval(string) |
解说 | 将string看作Perl语句执行。 正确执行后,系统变量$@为空串,如果有错误,$@中为错误信息。 |
例子 | $print = "print (/"hello,world//n/");"; eval ($print); |
结果输出 | hello, world |
函数名 | system |
调用语法 | system(list) |
解说 | list中第一个元素为程序名,其余为参数。 system启动一个进程运行程序并等待其结束,程序结束后错误代码左移八位成为返回值。 |
例子 | @proglist = ("echo", "hello,world!"); system(@proglist); |
结果输出 | hello, world! |
函数名 | fork |
调用语法 | procid = fork(); |
解说 | 创建程序的两个拷贝--父进程和子进程--同时运行。子进程返回零,父进程返回非零值,此值为子程序的进程ID号。 |
例子 | $retval = fork(); if ($retval == 0) { # this is the child process exit; # this terminates the child process } else { # this is the parent process } |
结果输出 | 无 |
函数名 | pipe |
调用语法 | pipe (infile, outfile); |
解说 | 与fork合用,给父进程和子进程提供通信的方式。送到outfile文件变量的信息可以通过infile文件变量读取。步骤: 1、调用pipe 2、用fork将程序分成父进程和子进程 3、一个进程关掉infile,另一个关掉outfile |
例子 | pipe (INPUT, OUTPUT); $retval = fork(); if ($retval != 0) { # this is the parent process close (INPUT); print ("Enter a line of input:/n"); $line = <STDIN>; print OUTPUT ($line); } else { # this is the child process close (OUTPUT); $line = <INPUT>; print ($line); exit (0); } |
结果输出 | $ program Enter a line of input: Here is a test line Here is a test line $ |
函数名 | exec |
调用语法 | exec (list); |
解说 | 与system类似,区别是启动新进程前结束当前程序。常与fork合用,当fork分成两个进程后,子进程用exec启动另一个程序。 |
例子 | |
结果输出 |
函数名 | syscall |
调用语法 | syscall (list); |
解说 | 调用系统函数,list第一个元素是系统调用名,其余为参数。 如果参数是数字,就转化成C的整型数(type int)。否则传递字符串的指针。详见UNIX的帮助或Perl文档。 使用syscall必须包含文件syscall.pl,即: require ("syscall.ph"); |
例子 | |
结果输出 |
函数名 | die |
调用语法 | die (message); |
解说 | 终止程序并向STDERR输出错误信息。message可以为字符串或列表。如果最后一个参数不包含换行符,则程序文件名和行号也被输出。 |
例子 | die ("Cannot open input file"); |
结果输出 | Cannot open input file at myprog line 6. |
函数名 | warn |
调用语法 | warn (message); |
解说 | 与die类似,区别是不终止程序。 |
例子 | warn("Danger! Danger!/n"); |
结果输出 | Danger! Danger! |
函数名 | exit |
调用语法 | exit (retcode); |
解说 | 终止程序并指定返回值。 |
例子 | exit(2); |
结果输出 | 无 |
函数名 | kill |
调用语法 | kill (signal, proclist); |
解说 | 给一组进程发送信号。 signal是发送的数字信号,9为杀掉进程。 proclist是进程ID列表。详见kill的UNIX帮助。 |
例子 | |
结果输出 |
函数名 | sleep |
调用语法 | sleep (time); |
解说 | 将程序暂停一段时间。time是停止的秒数。返回值为实际停止的秒数。 |
例子 | sleep (5); |
结果输出 | 无 |
函数名 | wait |
调用语法 | procid = wait(); |
解说 | 暂停程序执行,等待子进程终止。 不需要参数,返回值为子进程ID,如果没有子进程,返回-1。 |
例子 | |
结果输出 |
函数名 | waitpid |
调用语法 | waitpid (procid, waitflag); |
解说 | 暂停程序执行,等待特定的子进程终止。procid为等待的进程ID |
例子 | $procid = fork(); if ($procid == 0) { # this is the child process print ("this line is printed first/n"); exit(0); } else { # this is the parent process waitpid ($procid, 0); print ("this line is printed last/n"); } |
结果输出 | $ program this line is printed first this line is printed last $ |
函数名 | caller |
调用语法 | subinfo = caller(); |
解说 | 返回调用者的程序名和行号,用于Perl Debugger。 返回值为三元素的列表: 1、调用处的包名 2、调用者文件名 3、调用处的行号 |
例子 | |
结果输出 |
函数名 | chroot |
调用语法 | chroot (dir); |
解说 | 改变程序的根目录,详见chroot帮助。 |
例子 | |
结果输出 |
函数名 | local |
调用语法 | local($variable); |
解说 | 在语句块(由大括号包围的语句集合)中定义局域变量,仅在此语句块中起作用,对其的改变不对块外同名变量造成影响。 千万不要在循环中使用,否则每次循环都定义一个新的局域变量! |
例子 | |
结果输出 |
函数名 | times |
调用语法 | timelist = times |
解说 | 返回该程序及所有子进程消耗的工作时间。 返回值为四个浮点数的列表: 1、程序耗用的用户时间 2、程序耗用的系统时间 3、子进程耗用的用户时间 4、子进程耗用的系统时间 |
例子 | |
结果输出 |
函数名 | sin |
调用语法 | retval = sin (value); |
解说 | 参数为弧度值。 |
函数名 | cos |
调用语法 | retval = cos (value); |
解说 | 参数为弧度值。 |
函数名 | atan2 |
调用语法 | retval = atan2 (value1, value2); |
解说 | 运算并返回value1除以value2结果的arctan值,单位为弧度,范围在-PI~PI。 |
应用例: 角度转化成弧度子程序。 | sub degrees_to_radians { local ($degrees) = @_; local ($radians);11: $radians = atan2(1,1) * $degrees / 45; } |
函数名 | sqrt |
调用语法 | retval = sqrt (value); |
解说 | 平方根函数。value为非负数。 |
函数名 | exp |
调用语法 | retval = exp (value); |
解说 | 返回e的value次方。 |
函数名 | log |
调用语法 | retval = log (value); |
解说 | 以e为底的自然对数。 |
函数名 | abs |
调用语法 | retval = abs (value); |
解说 | 绝对值函数。(Perl 4中没有) |
函数名 | rand |
调用语法 | retval = rand (num); |
解说 | 随机数函数,返回0和整数num之间的一个浮点数。 |
函数名 | srand |
调用语法 | srand (value); |
解说 | 初始化随机数生成器。保证每次调用rand真正随机。 |
函数名 | index |
调用语法 | position = index (string, substring, position); |
解说 | 返回子串substring在字符串string中的位置,如果不存在则返回-1。参数position是可选项,表示匹配之前跳过的字符数,或者说从该位置开始匹配。 |
函数名 | rindex |
调用语法 | position = rindex (string, substring, position); |
解说 | 与index类似,区别是从右端匹配。 |
函数名 | length |
调用语法 | num = length (string); |
解说 | 返回字符串长度,或者说含有字符的数目。 |
函数名 | pos |
调用语法 | offset = pos(string); |
解说 | 返回最后一次模式匹配的位置。 |
函数名 | substr |
调用语法 | substr (expr, skipchars, length) |
解说 | 抽取字符串(或表达式生成的字符串)expr中的子串,跳过skipchars个字符,或者说从位置skipchars开始抽取子串(第一个字符位置为0),子串长度为length,此参数可忽略,意味着取剩下的全部字符。 当此函数出现在等式左边时,expr必须为变量或数组元素,此时其中部分子串被等式右边的值替换。 |
函数名 | study |
调用语法 | study (scalar); |
解说 | 用一种内部格式提高变量的访问速度,同一时刻只对一个变量起作用。 |
函数名 | lc uc |
调用语法 | retval = lc(string); retval = uc(string); |
解说 | 将字符串全部转换成小/大写字母。 |
函数名 | lcfirst ucfirst |
调用语法 | retval = lcfirst(string); retval = ucfirst(string); |
解说 | 将第一个字母转换成小/大写。 |
函数名 | quotameta |
调用语法 | newstring = quotemeta(oldstring); |
解说 | 将非单词的字母前面加上反斜线(/)。 语句 : $string = quotemeta($string); 等效于:$string =~ s/(/W)///$1/g; 常用于模式匹配操作中,确保字符串中没有字符被看作匹配操作符。 |
函数名 | join |
调用语法 | join (joinstr, list); |
解说 | 把字符串列表(数组)组合成一个长的字符串,在每两个列表元素间插入串joinstr。 |
函数名 | sprintf |
调用语法 | sprintf (string, fields); |
解说 | 与printf类似,区别是结果不输出到文件,而作为返回值赋给变量。 |
例子 | $num = 26; $outstr = sprintf("%d = %x hexadecimal or %o octal/n",$num, $num, $num); print ($outstr); |
结果输出 | 26 = 1a hexadecimal or 32 octal |