perl学习笔记之常用系统函数列表
时间:2009-05-22 来源:h0000001
常用系统函数列表
指令:print
语法: print Filehandle LIST 说明: 这个Filehandle可以看作是在I/O之间的一个桥梁,可以利用Filehandle来做数据的读入与写出动作。STDOUT是代表从哪输出数据;例如从电脑的屏幕输出;STDERR是代表从哪输出错误的数据,例如从电脑的屏幕输出。在PERL语言中有三个标准的Filehandle: 1. STDIN(标准输入):是代表STDIN的Filehandle 2. STDOUT(标准输出):是代表STDOUT的Filehandle 3. STDERR(标准错误输出):是代表STDRR的Filehandle 如果要使用其它的Filehandle,就要用OPEN这个函数来打开一个Filehandle,我们可以用PRINT这个函数将LIST的数据输出给FILEHANDLE. 在介绍PRINT这个函数之前,先看看PRINT函数中特殊打印字符: 符号 作用 ------------------- \n 换行 \r 光标换行 \t tab键 \f 换页 \b 退回一格 \v 垂直tab键 \a 响铃Bell \e escape键 \007 十进制ASCII码 \xff 十六进制码 \c[ 控制字符 示例: print STDOUT "Hello,World!\n"; 语法: print LIST 说明: 如果省略Filehandle的话,就会把Filehandle内定为STDOUT,也就是会将LIST的数据内容显示在屏幕上。 示例: $url="www.chinaunix.net/~test" print "CU: $url\n"; 将会在屏幕上出现 "CU: www.chinaunix.net/~test", 如果想要让双引号内的变量失效,可以在变量的前面加上反斜线"\"符号。 如: print "CU: \$url\n"; 这样在屏幕上就显示 "CU: $url" 语法: print 说明: 同时省略Filehandle和LIST的话,就会以STDOUT为Filehandle,并会输出$_这个内定输出变量内容。如果$_变量是一个空字符串的话,就会显示一个空字符串。 示例: $_="China Unix\n"; print; 就会将 "China Unix"加上换行符显示在屏幕上。 指令:printf 语法: printf Filehandle LIST 说明: 在perl语言中也提供C语言中的printf的语法,用法和C语言中的用法一模一样。如果把Filehandle省略的话,也一样也把STDOUT当成是内定的Filehandle. 以下是printf函数中变换符号的字符。 符号 作用 ------------------- %c 字符 %s 字符串 %d 整数 %f 浮点数 %h 十六进制数 %o 八进制数 示例: printf("chmod %d %s\n","711","cgi"); 会将 "chmod 711 cgi"显示在屏幕上。 指令: chop 语法: chop($url) 说明: 把最后一个字符删除。 示例: $url="www.chinaunix.net/~test/"; print chop($url); 将在屏幕上显示 www.chinaunix.net/~testnbsp; 指令: split 语法: split(/pattern/,$text,limit) 其中/pattern/是文字处理模式,而limit代表要分割的个数,一般省略。 说明: 用一个指定的文字处理模式来分割$text字符串。 示例: $text = "Michael,Gevin,Mike"; @name=split(/,/,$text); #这时@name=("Michael","Gevin","Mike"). ($a,$b,$c)=split(/,/,$test); #这时$a="Michael";$b="Gevin";$c="Mike"; @name=split(/,/,$test,2); #这时@name=("Michael","Gevin"); 指令: keys 语法: keys(%hash) 说明: 取出关联哈希中全部的key. 示例: %hash=(1,"Mike",2,"Michael"); @key=keys(%hash); #这时@key=(1,2); 指令: values 语法: values(%hash) 说明: 取出关联哈希中全部的value. 示例: %hash=(1,"Mike",2,"Michael"); @value=values(%hash); #这时@value=("Mike","Michael"); 指令: reverse 语法: reverse(@array) 说明: 将数组@array中的元素由后到前重新排列。 示例: @back=("A","B","C","D","E"); @back=reverse(@back); #这时@back=("E","D",'C","B","A"); 指令: sort 语法: sort(@array) 说明: 将数组中的元素由小到大排序,如果要由大到小排序的话,要加上reverse这个函数。 示例: @abc=("d","b","c","a"); @abc=sort(@abc); #这时@abc=("a","b","c","d"); @abc=reverse sort @abc; 指令: length 语法: length($string) 说明: 求出字符串$string的字节(bytes)值。 示例: $string="Perl5"; $size=length $string; #这时 $size=5; 指令: substr 语法: substr($string,offset,length) 说明: offset代表起始字符的位置,length代表引用的字符串长度,如果省略length则代表从起始值到字符串的最后一个字符长度。而offset如果是负值的话,就会从字符串右边开始指定字符。 示例: $s=substr("perl5",2,2); #这时 $s = "rl"; $s=substr("perl5",2); #这时 $s = "rl5"; $s=substr("abcdefg",-5,3); #这时 $s = "cde"; 指令: index 语法: index($string,$substring,position) 说明: $substring是要寻找的字符;position代表从哪一个位置开始寻找,如果省略position就从头开始找起。并返回所要找寻的字符在字符串$string中的位置,如果在字符串$string中找不到$substring,则会返回-1这个值。 示例: $s=index("perl5","p"); #这时 $s = 0 $s=index("perl5","l",2); #这时 $s = 3 $s=index("perl5","Perl); #这时 $s = -1 指令: push 语法: push(@array,$string) 说明: 在数组@array的最后添加新的元素$string到数组@array中。 示例: @array=("one","two"); push(@array,"three"); #这时@array=("one","two","three"); 指令: pop 语法: pop(@array) 说明: 将数组@array的最后一个元素删除,并将删除的元素返回。 示例: @array=("one","two"); $rm=pop(@array); #这时 @array=("one");而 $rm="two"; 指令: unshift 语法: unshift(@array,$string) 说明: 在数组@array的第一个元素前添加新的元素$string到数组@array中。 示例: @array=("one","two"); unshift(@array,"three"); #这时@array=("three","one","two"); 指令: shift 语法: shift(@array) 说明: 将数组@array的第一个元素删除,并将删除的元素返回。 示例: @array=("one","two"); $rm=shift(@array); #这时 @array=("two"); 而 $rm="one"; 指令: join 语法: join($string,@array) 说明: 在数组@array的元素之间加上一指定的字符$string,并将结果返回。 示例: @array=("one","two","three"); $total=join(":",@array); #这时 $total="one:two:three"; 指令: grep 语法: grep(/pattern/,@array) 说明: 将符合文字处理模式(regular expression)的数组元素找出来,并返回列表。 示例: @array=("one","on","in"); $count=grep(/on/,@array); #这时$count=2; @result=grep(/on/,@array); #这时@result=("one","on"); 指令: hex 语法: hex($string) 说明: 将十六进制的数值转成十进制。 示例: $decimal=hex("ff"); #这时 $decimal=255; 指令: rand 语法: rand($interger) 说明: 常和函数srand搭配来取得一随机数,如果没有先宣告srand函数的话,则取出的常数值是一个固定值。这个语法会返回一个介于0到$interger之间的数值,如果$interger省略的话,则会返回一个介于0和1的数值。 示例: srand; #要先宣告srand函数,才能产生随机数的效果。 $int=rand(10); #$int的值会大于0而且小于10,如果希望产生的随机数是整数的话,就要加上int这个函数。 如:$int=int(rand(10)); perl -e 'srand; $int=rand(10); print $int ,"\n";' 指令: localtime 语法: localtime(time) 说明: 该函数将返回九个有关时间的元素,在写CGI应用程序的时候常会用到系统的时间,所以在此详细介始这个函数的用法。 示例: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time) 其中: $sec 代表秒数[0,59] $min 代表分数[0,59] $hour 代表小时数[0,23] $mday 代表是这个月的第几天 [1,31] $mon 代表月数[0,11],因此,$mon要加上1之后才能符合实际情况。 $year 从1990年算机的年数 $wday 从星期六算起,代表是在这周的第几天[0-6] $yday 从一月一日算起,代表是在这年中的第几天 [0,365] $isdst 只是一个flag 指令: die 语法: die LIST 说明: 会把LIST字符串显示出来,并退出程序。常和$!这个代表错误信息变量一起使用。 示例: open(FILE,"$filename") || die "Can not open file $!\n"; 如果打开文件失败的话,就会显示出错误的信息,之后再退出程序。 指令: open 语法1: open(filehandle,"$filename") 说明: 用于文件的打开(read only).这个filehandle可以把它看作是在I/O之间的桥梁,可以用FILEHANDLE来作出数据的读入与写出。开始可用open这个函数来打开一个指定的文件,接下来可以用<filehandle>来读取所打开文件的数据内容,最后一定要用close这个函数关闭这个之前打开的filehandle. 在CGI中,要打开文件的路径一定要写绝对路径。 示例: $filename="usr/abc.txt"; open(FILE,"$filename") or die "can not open file $filename\n"; #将<file>数据指定给变量$line(一行一行地) while($line=<FILE>){ print "$line"; } close(FILE); 语法2: open(filehandle,"<$filename") 说明: 这个语法也可以打开一个存在的文件(read only). 示例: $filename = "usr/abc.txt"; open(file,"<$filename") || die "Can not open file $filename\n"; @array=<file>; #将<file>的数据内容都指定给数组@array. close(file); print "@array"; #也会把abc.txt这个文件的内容显示出来。 语法3: open(filehandle,">$filename") 说明: 建立一个新的文件(write only),如果己经存在这个文件,就会把旧文件名覆盖掉。并可用print filehandle的方式将数据存到所打开的文件中。 示例: $filename = "/usr/abc.txt"; open(file,">$filename") || die "can not open file $filename\n"; print file "this is a new line1\n"; print file "this is a new line2\n"; close(file); #会将数据存在一个新文件中. 语法4: open(filehandle,">>$filename") 说明: 数据用追加的方式存入一文件中(write only),如果指定的文件名不存在,就会新建立一个新的文件。 示例: $filename = "/usr/abc.txt"; open(file,">>$filename") or die "can not open file $filename\n"; print file "this is a new line1\n"; print file "this is a new line2\n"; close(file); #会把数据追加(append)到一个文件(abc.txt)中。 语法5: open(filehandle,"|unix command") 说明: 把filehandle中的数据输出给unix的指令来处理。 示例: $mailprog="/usr/bin/mail"; #unix系统上的寄信程序(一定要写绝对路径) $who="[email protected]"; open(file,"|$mailprog $who") || die "open failed!\n"; print file "I Love you!\n"; print file "I want to see you.\n"; close(file); #这样,就会通过unix系统mail程序,将file这个filehandle的数据内容寄给$who这个变量所指定的收信人。 指令: close 语法: close(filehandle) 说明: 用open这个函数打开一个filehandle之后,一定要用close这个函数把所打开的filehandle关闭. 示例: open(filehandle,"$filename"); close(filehandle); 指令: pack 语法: pack("指定的格式",LIST) 说明: pack这个函数会将一个LIST变成所指定的二进制数据格式。在CGI割解码过程中,会用到pack这个函数。 示例: $string=pack("c",65); #这时$string="a"; 将65这个ascii码转换成一个unsigned字符,其中c就是指定要转换成unsigned字符的意思。 指令: read 语法: read(filehandle,$string,length) 说明: 其中length代表读入字符串的长度(bytes).用read这个函数把filehandle中的数据依指定的字符串长度读入之后指派给$string这个变量。在cgi程序分割解码过程中,如果FORM的传送方式是设定为POST的话,就会将传送的数据设定为标准输入,所以会将数据内容指定给STDIN这个标准输入的filehandle,而CGI环境变量$env{'content_length'}就是代表使用者送出数据内容的长度,因此我们要用read这个函数来取得使用者送出的数据内容。 示例: read(stdin,$buffer,$env{'content_length'}); #将stdin这个标准输入filehandle中的数据依指定的字符串长度读入,再指派给$buffer这个变量。 指令: exit 语法: exit 说明: 退出执行的程序. 示例: print "i love cgi\n"; exit; #显示完"i love cgi"以后,将退出这个程序。
转自 http://www.west263.com/info/html/wangluobiancheng/qita/20080411/63938_3.html
语法: print Filehandle LIST 说明: 这个Filehandle可以看作是在I/O之间的一个桥梁,可以利用Filehandle来做数据的读入与写出动作。STDOUT是代表从哪输出数据;例如从电脑的屏幕输出;STDERR是代表从哪输出错误的数据,例如从电脑的屏幕输出。在PERL语言中有三个标准的Filehandle: 1. STDIN(标准输入):是代表STDIN的Filehandle 2. STDOUT(标准输出):是代表STDOUT的Filehandle 3. STDERR(标准错误输出):是代表STDRR的Filehandle 如果要使用其它的Filehandle,就要用OPEN这个函数来打开一个Filehandle,我们可以用PRINT这个函数将LIST的数据输出给FILEHANDLE. 在介绍PRINT这个函数之前,先看看PRINT函数中特殊打印字符: 符号 作用 ------------------- \n 换行 \r 光标换行 \t tab键 \f 换页 \b 退回一格 \v 垂直tab键 \a 响铃Bell \e escape键 \007 十进制ASCII码 \xff 十六进制码 \c[ 控制字符 示例: print STDOUT "Hello,World!\n"; 语法: print LIST 说明: 如果省略Filehandle的话,就会把Filehandle内定为STDOUT,也就是会将LIST的数据内容显示在屏幕上。 示例: $url="www.chinaunix.net/~test" print "CU: $url\n"; 将会在屏幕上出现 "CU: www.chinaunix.net/~test", 如果想要让双引号内的变量失效,可以在变量的前面加上反斜线"\"符号。 如: print "CU: \$url\n"; 这样在屏幕上就显示 "CU: $url" 语法: print 说明: 同时省略Filehandle和LIST的话,就会以STDOUT为Filehandle,并会输出$_这个内定输出变量内容。如果$_变量是一个空字符串的话,就会显示一个空字符串。 示例: $_="China Unix\n"; print; 就会将 "China Unix"加上换行符显示在屏幕上。 指令:printf 语法: printf Filehandle LIST 说明: 在perl语言中也提供C语言中的printf的语法,用法和C语言中的用法一模一样。如果把Filehandle省略的话,也一样也把STDOUT当成是内定的Filehandle. 以下是printf函数中变换符号的字符。 符号 作用 ------------------- %c 字符 %s 字符串 %d 整数 %f 浮点数 %h 十六进制数 %o 八进制数 示例: printf("chmod %d %s\n","711","cgi"); 会将 "chmod 711 cgi"显示在屏幕上。 指令: chop 语法: chop($url) 说明: 把最后一个字符删除。 示例: $url="www.chinaunix.net/~test/"; print chop($url); 将在屏幕上显示 www.chinaunix.net/~testnbsp; 指令: split 语法: split(/pattern/,$text,limit) 其中/pattern/是文字处理模式,而limit代表要分割的个数,一般省略。 说明: 用一个指定的文字处理模式来分割$text字符串。 示例: $text = "Michael,Gevin,Mike"; @name=split(/,/,$text); #这时@name=("Michael","Gevin","Mike"). ($a,$b,$c)=split(/,/,$test); #这时$a="Michael";$b="Gevin";$c="Mike"; @name=split(/,/,$test,2); #这时@name=("Michael","Gevin"); 指令: keys 语法: keys(%hash) 说明: 取出关联哈希中全部的key. 示例: %hash=(1,"Mike",2,"Michael"); @key=keys(%hash); #这时@key=(1,2); 指令: values 语法: values(%hash) 说明: 取出关联哈希中全部的value. 示例: %hash=(1,"Mike",2,"Michael"); @value=values(%hash); #这时@value=("Mike","Michael"); 指令: reverse 语法: reverse(@array) 说明: 将数组@array中的元素由后到前重新排列。 示例: @back=("A","B","C","D","E"); @back=reverse(@back); #这时@back=("E","D",'C","B","A"); 指令: sort 语法: sort(@array) 说明: 将数组中的元素由小到大排序,如果要由大到小排序的话,要加上reverse这个函数。 示例: @abc=("d","b","c","a"); @abc=sort(@abc); #这时@abc=("a","b","c","d"); @abc=reverse sort @abc; 指令: length 语法: length($string) 说明: 求出字符串$string的字节(bytes)值。 示例: $string="Perl5"; $size=length $string; #这时 $size=5; 指令: substr 语法: substr($string,offset,length) 说明: offset代表起始字符的位置,length代表引用的字符串长度,如果省略length则代表从起始值到字符串的最后一个字符长度。而offset如果是负值的话,就会从字符串右边开始指定字符。 示例: $s=substr("perl5",2,2); #这时 $s = "rl"; $s=substr("perl5",2); #这时 $s = "rl5"; $s=substr("abcdefg",-5,3); #这时 $s = "cde"; 指令: index 语法: index($string,$substring,position) 说明: $substring是要寻找的字符;position代表从哪一个位置开始寻找,如果省略position就从头开始找起。并返回所要找寻的字符在字符串$string中的位置,如果在字符串$string中找不到$substring,则会返回-1这个值。 示例: $s=index("perl5","p"); #这时 $s = 0 $s=index("perl5","l",2); #这时 $s = 3 $s=index("perl5","Perl); #这时 $s = -1 指令: push 语法: push(@array,$string) 说明: 在数组@array的最后添加新的元素$string到数组@array中。 示例: @array=("one","two"); push(@array,"three"); #这时@array=("one","two","three"); 指令: pop 语法: pop(@array) 说明: 将数组@array的最后一个元素删除,并将删除的元素返回。 示例: @array=("one","two"); $rm=pop(@array); #这时 @array=("one");而 $rm="two"; 指令: unshift 语法: unshift(@array,$string) 说明: 在数组@array的第一个元素前添加新的元素$string到数组@array中。 示例: @array=("one","two"); unshift(@array,"three"); #这时@array=("three","one","two"); 指令: shift 语法: shift(@array) 说明: 将数组@array的第一个元素删除,并将删除的元素返回。 示例: @array=("one","two"); $rm=shift(@array); #这时 @array=("two"); 而 $rm="one"; 指令: join 语法: join($string,@array) 说明: 在数组@array的元素之间加上一指定的字符$string,并将结果返回。 示例: @array=("one","two","three"); $total=join(":",@array); #这时 $total="one:two:three"; 指令: grep 语法: grep(/pattern/,@array) 说明: 将符合文字处理模式(regular expression)的数组元素找出来,并返回列表。 示例: @array=("one","on","in"); $count=grep(/on/,@array); #这时$count=2; @result=grep(/on/,@array); #这时@result=("one","on"); 指令: hex 语法: hex($string) 说明: 将十六进制的数值转成十进制。 示例: $decimal=hex("ff"); #这时 $decimal=255; 指令: rand 语法: rand($interger) 说明: 常和函数srand搭配来取得一随机数,如果没有先宣告srand函数的话,则取出的常数值是一个固定值。这个语法会返回一个介于0到$interger之间的数值,如果$interger省略的话,则会返回一个介于0和1的数值。 示例: srand; #要先宣告srand函数,才能产生随机数的效果。 $int=rand(10); #$int的值会大于0而且小于10,如果希望产生的随机数是整数的话,就要加上int这个函数。 如:$int=int(rand(10)); perl -e 'srand; $int=rand(10); print $int ,"\n";' 指令: localtime 语法: localtime(time) 说明: 该函数将返回九个有关时间的元素,在写CGI应用程序的时候常会用到系统的时间,所以在此详细介始这个函数的用法。 示例: ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time) 其中: $sec 代表秒数[0,59] $min 代表分数[0,59] $hour 代表小时数[0,23] $mday 代表是这个月的第几天 [1,31] $mon 代表月数[0,11],因此,$mon要加上1之后才能符合实际情况。 $year 从1990年算机的年数 $wday 从星期六算起,代表是在这周的第几天[0-6] $yday 从一月一日算起,代表是在这年中的第几天 [0,365] $isdst 只是一个flag 指令: die 语法: die LIST 说明: 会把LIST字符串显示出来,并退出程序。常和$!这个代表错误信息变量一起使用。 示例: open(FILE,"$filename") || die "Can not open file $!\n"; 如果打开文件失败的话,就会显示出错误的信息,之后再退出程序。 指令: open 语法1: open(filehandle,"$filename") 说明: 用于文件的打开(read only).这个filehandle可以把它看作是在I/O之间的桥梁,可以用FILEHANDLE来作出数据的读入与写出。开始可用open这个函数来打开一个指定的文件,接下来可以用<filehandle>来读取所打开文件的数据内容,最后一定要用close这个函数关闭这个之前打开的filehandle. 在CGI中,要打开文件的路径一定要写绝对路径。 示例: $filename="usr/abc.txt"; open(FILE,"$filename") or die "can not open file $filename\n"; #将<file>数据指定给变量$line(一行一行地) while($line=<FILE>){ print "$line"; } close(FILE); 语法2: open(filehandle,"<$filename") 说明: 这个语法也可以打开一个存在的文件(read only). 示例: $filename = "usr/abc.txt"; open(file,"<$filename") || die "Can not open file $filename\n"; @array=<file>; #将<file>的数据内容都指定给数组@array. close(file); print "@array"; #也会把abc.txt这个文件的内容显示出来。 语法3: open(filehandle,">$filename") 说明: 建立一个新的文件(write only),如果己经存在这个文件,就会把旧文件名覆盖掉。并可用print filehandle的方式将数据存到所打开的文件中。 示例: $filename = "/usr/abc.txt"; open(file,">$filename") || die "can not open file $filename\n"; print file "this is a new line1\n"; print file "this is a new line2\n"; close(file); #会将数据存在一个新文件中. 语法4: open(filehandle,">>$filename") 说明: 数据用追加的方式存入一文件中(write only),如果指定的文件名不存在,就会新建立一个新的文件。 示例: $filename = "/usr/abc.txt"; open(file,">>$filename") or die "can not open file $filename\n"; print file "this is a new line1\n"; print file "this is a new line2\n"; close(file); #会把数据追加(append)到一个文件(abc.txt)中。 语法5: open(filehandle,"|unix command") 说明: 把filehandle中的数据输出给unix的指令来处理。 示例: $mailprog="/usr/bin/mail"; #unix系统上的寄信程序(一定要写绝对路径) $who="[email protected]"; open(file,"|$mailprog $who") || die "open failed!\n"; print file "I Love you!\n"; print file "I want to see you.\n"; close(file); #这样,就会通过unix系统mail程序,将file这个filehandle的数据内容寄给$who这个变量所指定的收信人。 指令: close 语法: close(filehandle) 说明: 用open这个函数打开一个filehandle之后,一定要用close这个函数把所打开的filehandle关闭. 示例: open(filehandle,"$filename"); close(filehandle); 指令: pack 语法: pack("指定的格式",LIST) 说明: pack这个函数会将一个LIST变成所指定的二进制数据格式。在CGI割解码过程中,会用到pack这个函数。 示例: $string=pack("c",65); #这时$string="a"; 将65这个ascii码转换成一个unsigned字符,其中c就是指定要转换成unsigned字符的意思。 指令: read 语法: read(filehandle,$string,length) 说明: 其中length代表读入字符串的长度(bytes).用read这个函数把filehandle中的数据依指定的字符串长度读入之后指派给$string这个变量。在cgi程序分割解码过程中,如果FORM的传送方式是设定为POST的话,就会将传送的数据设定为标准输入,所以会将数据内容指定给STDIN这个标准输入的filehandle,而CGI环境变量$env{'content_length'}就是代表使用者送出数据内容的长度,因此我们要用read这个函数来取得使用者送出的数据内容。 示例: read(stdin,$buffer,$env{'content_length'}); #将stdin这个标准输入filehandle中的数据依指定的字符串长度读入,再指派给$buffer这个变量。 指令: exit 语法: exit 说明: 退出执行的程序. 示例: print "i love cgi\n"; exit; #显示完"i love cgi"以后,将退出这个程序。
转自 http://www.west263.com/info/html/wangluobiancheng/qita/20080411/63938_3.html
相关阅读 更多 +
排行榜 更多 +