LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 16510|回复: 4

perl 函数集--------正在建设中

[复制链接]
发表于 2003-12-5 20:57:10 | 显示全部楼层 |阅读模式
下列的函数只是简单的使用例子。没什么解释,有些英语描述得不好,请见谅。大家不懂的就来这里看看吧,要随时添加,没什么顺序,找函数时可以用搜索就方便找到了。下面的只是一小部分,我的E文差,好的函数集的网址一直找不到,哪里有列出函数使用的网站,请推荐,也可以发到我的短信箱。:)

perl的主站---函数的帮助资料。
http://www.perldoc.com/perl5.6/pod/perlfunc.html

在网上找到的不全的一些函数。
http://member.netease.com/~elvis/docs/c11.htm
http://www.panjin.net/tybl/cgi/jiaocheng/cp3.htm
http://www.mstong.net/yehuo/tans ... =12&filename=55
http://www.mamiyami.com/doc/php/ref.strings.html


一些函数的解释
abs[php]
#!/usr/bin/perl -w
#$result=abs(value);
#
@re=abs(-23);
print "\@re\=abs\(\-23\)\;\n";
print "\@re\=@re\n";[/php]

array[php]
#!/usr/bin/perl -w
@num=qw(11 33 2 dd 0);
printf "firstnum\n";
$name="dear";
@list=(1..12,"hello",$name,"hello $name",4+6,1-2,2*3);
printf "secondlist\n";
@a=(a..z,A..Z,-2.3..8.1);
printf "threea\n";
printf "four:next\n";
@b=(1,"world",@num);
printf "third@b\n";
printf "sixnext\n";
@c=@num;
printf "sevre@c\n";[/php]

atan2[php]
#!/usr/bin/perl -w
#retval=atan2(value1,value2);
#
sub degress_to_radians {
my ($degress) = @_;
my ($radians);11;
$radians = atan2(1,1) * $degress /45;
}[/php]

chdir[php]
#!/usr/bin/perl -w
#change the current directory .
chdir("/root");
$result=system("ls");
print "$result\n";[/php]

chomp  AND chop[php]
#!/usr/bin/perl -w
use strict;
my @a="abcd";
print "@a\n--chomp--\n";
chomp(@a);
print "@a\n";
print "---chop---\n";
my @b="uiok";
print "@b\n";
chop(@b);
print "@b\n";[/php]

chr[php]
#!/usr/bin/perl -w
#@char=chr(asciivalue);
#
@a=chr(97);
print "@a\n";[/php]

each[php]
#!/usr/bin/perl -w
#@pair=each(%assoc_array);
#
print "\@pair\=each\(\%assoc\_array\)\;\n";
%array=(9,"first",2,"second");
@a=each(%array);
print "@a\n";

%arrayA=(8,"first",6,"second");
@b=each(%arrayA);
print "@b\n";

%arrayB=(12,"first",98,"second",66,"three",2,"found");
@d=each(%arrayB);
@e=each(%arrayB);
@f=each(%arrayB);
print "@d\n@e\n@f\n";[/php]

eof[php]
#!/usr/bin/perl -w
while ($line = <>) {
   print ($line);
   if (eof) {
   print ("-- end of current file --\n");
}
}[/php]

eval[php]
#!/usr/bin/perl -w
$print="print (\"hello,world\\n\");";
eval ($print);
#run as perl command.[/php]

foreach[php]
#!/usr/bin/perl -w
@array=("how","do","you","do" , "fine");
foreach $a(@array){
$a=~s/o/CHANGE/;
print "$a\n";
};
#
open (FILE, "test03.pl" );
foreach (<FILE> ) {
print "$_";
}[/php]

fork[php]
#!/usr/bin/perl -w
$result=fork();
if($result == 0) {
                 #this is the child process
exit; #this is terminates the child process
}else{
#this is the parent process
}[/php]

forka[php]
#!/usr/bin/perl -w
$child=fork();
print "$child\n";[/php]

format[php]
#!/usr/bin/perl -w
#file :format
#
$~="myformat";
write;
format myformat=
========================
hello,the world!
========================
.[/php]

getc AND die[php]
#!/usr/bin/perl -w
open(FILE,"/etc/fstab") or die "could not open /etc/fstab!";
print (getc(FILE),"");
$a=getc(FILE);
#chomp($a);
print "$a";
print (getc(FILE));
print (getc(FILE)."\n");[/php]

grep[php]
#!/usr/bin/perl -w
#@foundlist = grep (pattern, @searchlist);
#
@list = ("This", "is", "a", "test");
@foundlist = grep(/^[tT]/, @list);
print "@list\n@foundlist\n";
#
#result:::
#This is a test
#This test[/php]

hash
#!/usr/bin/perl -w
%a=(1,"a",2,"b",3,"c",4,"d",5,"e",6,"f");
$b=$a{1}; print "$b\n";
#
$a{2}="change"; $b=$a{2}; print "$b\n";
#
@index=keys(%a); print "@index\n";
#
@content=values(%a); print "@content\n";
#
@d=%a; print "@d\n";
#
delete $a{5}; @d=%a; print "@d\n";
#
$i="a"; $j=1; delete ${$i}{$j}; @d=%a; print "@d\n";


hex[php]
#!/usr/bin/perl -w
#16 format to 10 format number.
#result=hex(16 format);
#
@a=hex(032);
print "@a\n";[/php]

index[php]
#!/usr/bin/perl -w
#position=index(string,substring,position);
#
@re=index("1234567","123");
print "@re\n";
@re=index("1234567","6","3");
print "@re\n";[/php]

int[php]
#!/usr/bin/perl -w
@a=int(2.39);
print "@a\n";[/php]

join[php]
#!/usr/bin/perl -w
#join (joinstr,list);
#
@a=("a","b","c","d");
print "@a\n";
@b=join("#",@a);
print "@b\n";[/php]

keys[php]
#!/usr/bin/perl -w
#@list=keys(%assoc_array);
%NAME=(1,"mike",2,"michael");
@readkey=keys(%NAME);
print "%NAME\n";
print "@readkey\n";[/php]

kill[php]
#!/usr/bin/perl -w
#kill(signal,proclist);
#signal == signal ;example 9
#porclist == process ID
kill (9,1617);[/php]

last[php]
#!/usr/bin/perl -w
$a=12;
print "\$a\=$a\n";
while($a<25) {
$a++;
last if ($a == 20);
print "$a ";
}
print "\n";[/php]

lc[php]
#!/usr/bin/perl -w
@a=lc("ABC");
print "@a\n";[/php]

lcfirst[php]
#!/usr/bin/perl -w
#
# result=lcfirst(string);
@result=lcfirst("abcdefg");
print "@result\n";
@a=lcfirst("ABCD");
print "@a\n";[/php]

length[php]
#!/usr/bin/perl -w
#num=length(string);
#
@num=length("abcdefg");
print "@num\n";[/php]

log[php]
#!/usr/bin/perl -w
#result=log(value);
#
$re=log(12);
print "\$re\=log\(12\)\;\n";
print "\$re\=$re\n";[/php]

map[php]
#!/usr/bin/perl -w
#@resultlist=map(expr,@list);
#
@list=(50,3,1000);
print "@list\n";
@result=map($_+1,@list);
print "@result\n";
print "@list\n";[/php]

mkdir[php]
#!/usr/bin/perl -w
#4000: running setup user ID.
#2000: running setup groupp ID.
#1000: ease.
#0400: readable with own.
#0200: write with own.
#0200: can running with own.
#0040: readable group.
#0020: can write with group.
#0010: can running with group.
#0004: readable with all user.
#0002: write with all user.
#0001; running with all user.

mkdir("aka",0777) or die "Could not creat directory\n";[/php]

next[php]
#!/usr/bin/perl -w
$a=18;
while($a<23){
$a++;
next if ($a==20);
print "$a ";
}
print "$a\n";[/php]

oct[php]
#!/usr/bin/perl -w
#8 OR 16 format to 10 format
#@result=oct(octnum);
#
@a=oct("013");
print "@a\n";
@b=oct("0x1a");
print "@b\n";[/php]

opendir ADN readdir AND closedir[php]
#!/usr/bin/perl -w
opendir (DIR,"/root") or die "could not open /root";
@dots=grep {/^[^\.]/ && -d "/root/$_" } readdir(DIR);
foreach (@dots) {
print "$_\n";
}
closedir DIR;[/php]

ord[php]
#!/usr/bin/perl -w
#@result=ord("char");
#print a character ASCII value.
#
use strict;
my @a=ord("a");
print "@a\n";[/php]

pipe[php]
#!/usr/bin/perl -w
pipe(INPUT,OUTPUT);
$result=fork();
if($result != 0){
#this is cht 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);
}
#pipe as shell " | ".[/php]

pop[php]
#!/usr/bin/perl -w
#@element=pop(@array);
#
@array=("hello","the","world","dear");
print "@array\n";
@element=pop(@array);
print "@array\n@element\n";
[/php]

push[php]
#!/usr/bin/perl -w
#push(@arrayvar,elements);
#
@array=("hello","free","world");
print "@array\n";
push(@array,"my dear");
print "@array\n";
push(@array,"my dear too");
print "@array\n";[/php]

redo[php]
#!/usr/bin/perl -w
$a=15;
while ($a<19){
$a++;
print "$a ";
redo if ($a ==19);
}
print "\n";

$a=15;
while($a<=19){
$a++;
print "$a ";
redo if ($a ==19);
}
print "\n";[/php]

reverse[php]
#!/usr/bin/perl -w
use strict;
print "Enter the list of string:\n";
my $a=0;
my $into;
my @test;
while ( $a<6) {
chomp($into=<>);
unshift(@test,"$into");
$a++ ;
}
print "@test\n" ;
#
my @reverse=reverse(@test);
print "@reverse\n ";
my @b;
@b=reverse("a","b","c","d");
print "@b\n";[/php]

rindex[php]
#!/usr/bin/perl -w
#position =rindex(string,substring.position);
#from right to left
#
@a=rindex("abcdefg","e","b");
print "@a\n";
@a=rindex("abcdefg","e");
print "@a\n";[/php]

shift[php]
#!/usr/bin/perl -w
#element = shift (@arrayvar);
#
@array=("a","998","ojjo","iu");
print "@array\n";
@a=shift(@array);
print "@array\n@a\n";[/php]

shutdown
#Shuts down a socket connection in the manner indicated by HOW, which has the
#same interpretation as in the system call of the same name.
shutdown(SOCKET, 0);    # I/we have stopped reading data
shutdown(SOCKET, 1);    # I/we have stopped writing data
shutdown(SOCKET, 2);    # I/we have stopped using this socket


sleep[php]
#!/usr/bin/perl -w
@a=sleep (3);
print ("the process alerady sleep 3 second\n");
print "@a\n";
print ("return value is NULL\n");

print "\n";[/php]

sort[php]
#!/usr/bin/perl -w
@a=sort("a","b",1,3,6,0);
print "@a\n";[/php]

splice[php]
#!/usr/bin/perl -w
#@retval = splice (@array, slipelements, length, @newlist);
#if lenth=0;then insert a element.
#
@array=("a","b","9","8","K","ok");
@a=splice (@array, 2, 2, "Hello");
print "@a\n";[/php]

split[php]
#!/usr/bin/perl -w
#@list=split(parrern,sting,maxlength);
#
@text=("well","hello,the world","how","do","you","do");
print "@text\n";
@name=split(/,/,@text,2);
print "@name\n";
print "@text\n";
$abc = "apile:fjkdfk:300:500:XXX:/bin/bash";

@abc = split(/:/,$abc);
print "@abc\n";[/php]

sprintf[php]
#!/usr/bin/perl -w
#same like printf ,not ouput to file,return value to variable .
#
$num=26;
$outstr=sprintf("%d=%x hexadecimal or %o octal\n",$num,$num,$num);
print ($outstr);[/php]

sqrt[php]
#!/usr/bin/perl -w
#retval=sqrt(value);
#example
#value > 0;
$result=sqrt(9);
print "\$result\=sqrt\(9\)\;\n\$result\=$result\n";[/php]

srand AND rand[php]
#!/usr/bin/perl -w
#result=rand(num);
#
srand();
$re=rand(A);
print "$re\n";[/php]

system[php]
#!/usr/bin/perl -w
#system() ;run a shell commend.

$result=system "date";
$see=system("ls","/root");
print "$result\n";
print "$see\n";
$well=system "'date'";
print "$well\n";
@hello=("echo","hello,world!");
system(@hello);[/php]

uc[php]
#!/usr/bin/perl -w
@a=uc("abcd");
print "@a\n";[/php]

unless[php]
#!/usr/bin/perl -w
$a=12;
unless ($a!=12){
print "firsta\n";
}
unless($a==12){
print "seconda\n";
}[/php]

unlink[php]
#!/usr/bin/perl -w
#unlink("filename");
unlink("/share/perl/test");[/php]

unpack[php]
#!/usr/bin/perl -w
#@list = unpack (packformat, formatstr);
#
open (CODEDFILE, "/share/perl/function/aa") ||
die "Can't open input file";
open (OUTFILE, ">outfile") ||
die "Can't open output file";
while ($line = <CODEDFILE>) {
$decoded = unpack("u", $line);
print OUTFILE ($decoded);
}
close (OUTFILE);
close (CODEDFILE);[/php]

unshift[php]
#!/usr/bin/perl -w
#count = unshift (@arrayver, elements);
#
@array=("ui","ok","nb","li","well");
print "@array\n";
@a=unshift(@array,"first");
print "@array\n@a\n";[/php]

until[php]
#!/usr/bin/perl -w
$a=12;
until ( $a==18) {
$a++;
print "$a ";
}
print "\n";[/php]

values[php]
#!/usr/bin/perl -w
#@list=values(%assoc_array);
#
print "\@list\=values\(\%assoc\_array\)\;\n";
%NAME=(1,"mike",2,"michael");
@readval=values(%NAME);
print "@readval\n";[/php]

vec[php]
#!/usr/bin/perl -w
#retval = vec (vector, index, bits);
#
$vector = pack ("B*", "11010011");
$val1 = vec ($vector, 0, 4);
$val2 = vec ($vector, 1, 4);
print ("high-to-low order values: $val1 and $val2\n");
$vector = pack ("b*", "11010011");
$val1 = vec ($vector, 0, 4);
$val2 = vec ($vector, 1, 4);
print ("low-to-high order values: $val1 and $val2\n");[/php]

waitpid[php]
#!/usr/bin/perl -w
#wait for a sub_process,unit the sub_process done.
#procid is ID of sub_process
#format : waitpid(procid,witflay);
#example:
$procid=fork();
if ($procid == 0){
#this is the child porcess
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");
}[/php]

wantarray[php]
#!/usr/bin/perl -w
#result=wantarray();

@array = &mysub();
$scalar = &mysub();

sub mysub {
    if (wantarray()) {
       print ("true\n");
     } else {
     print ("false\n");
     }
}[/php]

while[php]
#!/usr/bin/perl -w
use strict;
open(FILE, "/etc/fstab" );
my $line;
while ( $line=<FILE>) {
print "$line";
}[/php]

umask[php]
umask(0111);[/php]

不知道大家喜欢我这样的做吗?
发表于 2003-12-5 21:02:21 | 显示全部楼层
呵呵~~,很实用~~, 不过在<<perl技术内幕>>中有介绍了很多常用的函数和特殊变量~~,值得一看~~
 楼主| 发表于 2003-12-5 21:09:41 | 显示全部楼层
最初由 javalee 发表
呵呵~~,很实用~~, 不过在<<perl技术内幕>>中有介绍了很多常用的函数和特殊变量~~,值得一看~~


没有好书呀!!不知道什么时候才能打好基础   



devel 很認真的說"注意:从现在开始,请不要置顶贴子闲谈和灌水,谢谢合作。"
发表于 2006-8-23 17:58:55 | 显示全部楼层
能不能把各个函数的用途简单描述一下啊。
回复 支持 反对

使用道具 举报

发表于 2006-8-23 19:04:36 | 显示全部楼层
自己看看 perldoc 吧。。

比如 perldoc -f split
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表