升木 发表于 2017-5-19 07:50:11

Perl中经常使用的一些方法

检测操作系统的类型
print "$^O\n";
linux on Linux and MSWin32 on Windows
use English qw' -no_match_vars ';
print "$OSNAME\n";

use Config;
print "$Config{osname}\n";
print "$Config{archname}\n";
http://stackoverflow.com/questions/334686/how-can-i-detect-the-operating-system-in-perl

取得文件名称
my $fileName = $file; # /tmp/test.log
$fileName =~ s!^.*(\\|\/)!!; # 得到fileName - "test.log"

删除目录下的文件
-d $_?rmtree($_):unlink $_ foreach (<data/*>);

文件上传
      if (!open(OUTFILE, ">$baseDir/$fileName")) {
print "Cannot open the file $baseDir/$fileName";
exit;
}
while (my $bytesRead = read($_, my $buffer, 1024)) {
print OUTFILE $buffer;
}
close (OUTFILE);

去掉字符串两边的空格或回车换行
sub trim_str
{
my $str = shift;
if ($$str) {
$$str =~ s/^\s+//g;
$$str =~ s/\s+$//g;
$$str =~ s/[\r\n]//g;
}
return $str;
}

日期格式化
use POSIX qw(strftime);
my $time = strftime("%Y%m%d%H%M%S", localtime(time));
http://blog.chinaunix.net/uid-608135-id-2948410.html
页: [1]
查看完整版本: Perl中经常使用的一些方法