perl常用功能小汇总

验证邮箱:

 

my $email_Address = "8\@qcn";
if ($email_Address =~ /^(\w)+@(\w)+(\.\w+)+$/i) {
    print "ok ";
} else {
    print "err";
}

 

字符串长度

 

my $a = "秋水09";
$len = rindex ($a."\$", "\$");
print $len;

 

字符串分隔

 

my @array = split(/#/,$line);

 

字符串连接

 

@array=qw(one two three);
$total=join(":",@array)

 

去掉字符串中的空格(正则)

 

$line =~ s/\s//g;

 

目录遍历

 

my $dir = "/root/test/";
if (! opendir(MYDIR, $dir)) {
    die ("cannot open input dir\n");
}
while (my $line=readdir MYDIR) {
    chomp($line); #去除换行
    if ($line eq "." or $line eq "..") {
        next;
    }
    print $line."\n";
}

 

文件内容MD5

 

use Digest::MD5;  
  my $img = “test.jpg”;

    if (! open(MYFILE, $img)) {
        next;
    }
    binmode(MYFILE);
    my $md5 = Digest::MD5->new->addfile(*MYFILE)->hexdigest;

判断汉字

 

if($line=~ /[\x80-\xFF][\x40-\xFF]/)
 {
 } 

产生随机数

 

my $num;
 for(1..6) {
        $num .= int (rand(9));
 }

写入文件

 

if(open(TIAN,">>test.txt")){
            syswrite(TIAN,"write\n");
            close(TIAN);
        }

 

Tags: perl

上一篇: 尽量通过left进行CSS定位提示层   下一篇: Cannot assign requested address

提交疑问

回顶部