perl 常用函数以及例子代码

来源:互联网 发布:软件在线制作 编辑:程序博客网 时间:2024/04/28 23:12

abs
PERL 代码:
#!/usr/bin/perl -w
#$result=abs(value);
#
@re=abs(-23);
print "/@re/=abs/(/-23/)/;/n";
print "/@re/=@re/n"; 

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

atan2
PERL 代码:
#!/usr/bin/perl -w
#retval=atan2(value1,value2);
#
sub degress_to_radians {
my ($degress) = @_;
my ($radians);11;
$radians = atan2(1,1) * $degress /45;


chdir
PERL 代码:
#!/usr/bin/perl -w
#change the current directory .
chdir("/root");
$result=system("ls");
print "$result/n"; 

chomp AND chop
PERL 代码:
#!/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"; 
在预读Perl程序代码的时候,也许你会发现Coder有时会用chop()函数来去掉<>输入的换行符"/n",有时候使用chomp()函数。

  其实上面的用法都可以达到预期的效果,但它们也有细微的差别。

 
  chop()函数,只是去除最后一个字符。
  chomp()函数,就要首先判断最后一个字符是不是为"/n",他才去除。

 


chr
PERL 代码:
#!/usr/bin/perl -w
#@char=chr(asciivalue);
#
@a=chr(97);
print "@a/n"; 

each
PERL 代码:
#!/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"; 

eof
PERL 代码:
#!/usr/bin/perl -w
while ($line = ) {
print ($line);
if (eof) {
print ("-- end of current file --/n");
}
}

 

eval
PERL 代码:
#!/usr/bin/perl -w
$print="print (/"hello,world//n/");";
eval ($print);
#run as perl command. 

foreach
PERL 代码:
#!/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 ( ) {
print "$_";


fork
PERL 代码:
#!/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


forka
PERL 代码:
#!/usr/bin/perl -w
$child=fork();
print "$child/n"; 

format
PERL 代码:
#!/usr/bin/perl -w
#file :format
#
$~="myformat";
write;
format myformat=
========================
hello,the world!
========================


getc AND die
PERL 代码:
#!/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"); 

grep
PERL 代码:
#!/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 

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
PERL 代码:
#!/usr/bin/perl -w
#16 format to 10 format number.
#result=hex(16 format);
#
@a=hex(032);
print "@a/n"; 

index
PERL 代码:
#!/usr/bin/perl -w
#position=index(string,substring,position);
#
@re=index("1234567","123");
print "@re/n";
@re=index("1234567","6","3");
print "@re/n"; 

int
PERL 代码:
#!/usr/bin/perl -w
@a=int(2.39);
print "@a/n"; 

join
PERL 代码:
#!/usr/bin/perl -w
#join (joinstr,list);
#
@a=("a","b","c","d");
print "@a/n";
@b=join("#",@a);
print "@b/n"; 

keys
PERL 代码:
#!/usr/bin/perl -w
#@list=keys(%assoc_array);
%NAME=(1,"mike",2,"michael");
@readkey=keys(%NAME);
print "%NAME/n";
print "@readkey/n"; 

kill
PERL 代码:
#!/usr/bin/perl -w
#kill(signal,proclist);
#signal == signal ;example 9
#porclist == process ID
kill (9,1617); 

last
PERL 代码:
#!/usr/bin/perl -w
$a=12;
print "/$a/=$a/n";
while($a<25) {
$a++;
last if ($a == 20);
print "$a ";
}
print "/n"; 

lc
PERL 代码:
#!/usr/bin/perl -w
@a=lc("ABC");
print "@a/n"; 

lcfirst
PERL 代码:
#!/usr/bin/perl -w
#
# result=lcfirst(string);
@result=lcfirst("abcdefg");
print "@result/n";
@a=lcfirst("ABCD");
print "@a/n"; 

length
PERL 代码:
#!/usr/bin/perl -w
#num=length(string);
#
@num=length("abcdefg");
print "@num/n"; 

log
PERL 代码:
#!/usr/bin/perl -w
#result=log(value);
#
$re=log(12);
print "/$re/=log/(12/)/;/n";
print "/$re/=$re/n"; 

map
PERL 代码:
#!/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"; 

mkdir
PERL 代码:
#!/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"; 

next
PERL 代码:
#!/usr/bin/perl -w
$a=18;
while($a<23){
$a++;
next if ($a==20);
print "$a ";
}
print "$a/n"; 

oct
PERL 代码:
#!/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"; 

opendir ADN readdir AND closedir
PERL 代码:
#!/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; 

ord
PERL 代码:
#!/usr/bin/perl -w
#@result=ord("char");
#print a character ASCII value.
#
use strict;
my @a=ord("a");
print "@a/n"; 

pipe
PERL 代码:
#!/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 " | ". 

pop
PERL 代码:
#!/usr/bin/perl -w
#@element=pop(@array);
#
@array=("hello","the","world","dear");
print "@array/n";
@element=pop(@array);
print "@array/n@element/n"; 

push
PERL 代码:
#!/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"; 

redo
PERL 代码:
#!/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"; 

reverse
PERL 代码:
#!/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"; 

rindex
PERL 代码:
#!/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"; 

shift
PERL 代码:
#!/usr/bin/perl -w
#element = shift (@arrayvar);
#
@array=("a","998","ojjo","iu");
print "@array/n";
@a=shift(@array);
print "@array/n@a/n"; 

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
PERL 代码:
#!/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"; 

sort
PERL 代码:
#!/usr/bin/perl -w
@a=sort("a","b",1,3,6,0);
print "@a/n"; 

splice
PERL 代码:
#!/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"; 

split
PERL 代码:
#!/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"; 

sprintf
PERL 代码:
#!/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); 

sqrt
PERL 代码:
#!/usr/bin/perl -w
#retval=sqrt(value);
#example
#value > 0;
$result=sqrt(9);
print "/$result/=sqrt/(9/)/;/n/$result/=$result/n"; 

srand AND rand
PERL 代码:
#!/usr/bin/perl -w
#result=rand(num);
#
srand();
$re=rand(A);
print "$re/n"; 

system
PERL 代码:
#!/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); 

uc
PERL 代码:
#!/usr/bin/perl -w
@a=uc("abcd");
print "@a/n"; 

unless
PERL 代码:
#!/usr/bin/perl -w
$a=12;
unless ($a!=12){
print "first:$a/n";
}
unless($a==12){
print "second:$a/n";


unlink
PERL 代码:
#!/usr/bin/perl -w
#unlink("filename");
unlink("/share/perl/test"); 

unpack
PERL 代码:
#!/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 = ) {
$decoded = unpack("u", $line);

print OUTFILE ($decoded);
}
close (OUTFILE);
close (CODEDFILE); 

unshift
PERL 代码:
#!/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"; 

until
PERL 代码:
#!/usr/bin/perl -w
$a=12;
until ( $a==18) {
$a++;
print "$a ";
}
print "/n"; 

values
PERL 代码:
#!/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"; 

vec
PERL 代码:
#!/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"); 

waitpid
PERL 代码:
#!/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");


wantarray
PERL 代码:
#!/usr/bin/perl -w
#result=wantarray();

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

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


while
PERL 代码:
#!/usr/bin/perl -w
use strict;
open(FILE, "/etc/fstab" );
my $line;
while ( $line=) {
print "$line";


umask
PERL 代码:
umask(0111);

 

 

 

原创粉丝点击