Perl中经常使用的一些方法

[b][color=red]检测操作系统的类型[/color][/b]

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";

[url]http://stackoverflow.com/questions/334686/how-can-i-detect-the-operating-system-in-perl[/url]


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



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



[b][color=red]文件上传[/color][/b]
        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);



[color=red][b]去掉字符串两边的空格或回车换行[/b][/color]
sub trim_str
{
my $str = shift;

if ($$str) {
$$str =~ s/^\s+//g;
$$str =~ s/\s+$//g;
$$str =~ s/[\r\n]//g;
}

return $str;
}


[b][color=red]日期格式化[/color][/b]
use POSIX qw(strftime);
my $time = strftime("%Y%m%d%H%M%S", localtime(time));

[url]http://blog.chinaunix.net/uid-608135-id-2948410.html[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值