perl附录(1)[1]

[入库:2005年9月19日] [更新:2007年3月24日]

本文简介:

perl函数的参考手册,留着查阅方便。

一、进程处理函数

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 = ;
print OUTPUT ($line);
} else {
# this is the child process
close (OUTPUT);
$line = ;
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");
例子
结果输出

2、进程终止函数

函数名 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帮助。
例子
结果输出

3、进程控制函数

函数名 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
$

4、其它控制函数

函数名 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之间的一个浮点数。

本文关键:perl附录(1)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top