use strict;
use warnings;
use Encode;
use DBI; #关系型数据库
use MIME::Base64; #base64编解码
use MIME::QuotedPrint;? #可打印字符编码
use Encode;
use Cache::Memcached;? #memcache缓存库
use Getopt::Long;
=========================DBI============================
对于关系型数据库perl提供了DBI;??
1: 数据库链接:
???????? mysql:my $conn = DBI->connect("DBI:mysql:database=test:host=127.0.0.1","root","passwd",{'RaiseError' => 1});
???????? oracle:my $dbh = DBI->connect('test','root','passwd','oracle');?? if(!$dbh){?? print $DBI::errstr;? } #打印错误码
2:数据库操作:
???????? my $rows = $conn->do("delete from emptable where status != 'active'"); #如果多次查询,系统会对其一遍遍做语法分析
一般insert update delete等操作用$conn->do()即可
否则select时候要用prepare和execute结合 因为do只运行命令不返回结果,select要得到结果必须用prepare.
???????? my $sth = $dbh->prepare('insert into emptable (id,name) values (?,?)');?#该方式可以减轻上述分析负载,“?”表示查询的位置。
????????????? while(defined($line == <>)){? #prepare参数化查询
?????????????????????????chomp($line);? #去掉行末回车
?????????????????????????($id,$name) = split(/t/,$line);?#假设数据间用制表符分开
??????????????????????? ?$sth->execute($id,$name); #insert into mysql
???????????????????????? die $DBI::errstr if DBI::err;
???????????? }
??????? ?select查询:下面这段例子演示了如何使用数据库操作中最常用的查询。
??????? ?my $cur = $dbh->prepare('select name from emptable where id < 100');
???????????????$cur->execute();? #查询
?????????????? die $DBI::errstr if DBI::err;
?????????????? while(($name) = $cur->fetchrow){
???????????????????? ?? ?print "name: $name";? #循环打印查询结果
??????????????? }
??????? ?$cur->finish();
??????? $conn->quote($sender); //特殊字符转换 防止被黑的
???????? 3:断开连接:$dbh->disconnect();??
? ?????? 4:查询元数据:
?????????????? $DBI::rows?? #作用或返回的行数
?????????????? $sth->{NUM_FIELDS} #由select语句返回的字段数
?????????????? $sth->{NUM_PARAMS}? #由任意查询返回的参数个数
???????????????执行完select语句可以得到以下元数据信息:
?????????????? $sth->{NAME} #由查询所返回的列名称
?????????????? $sth->{NULLABLE} #表示字段是否可以为空的布尔值
?????????????? $sth->{TYPE} #字段类型
?????????????? $sth->{PRECISION}? #字段的浮点数精度
???????????????$sth->{SCALE} #字段长度
=========================Memcache============================
memcache操作:
memcached:my $memd = new Cache::Memcached{?? 'servers' => ['127.0.0.1:11211']?????? };
my $sys_sec = $memd->get("name”);? #取数据、
$memd->delete($opt{username});? #删数据
============================Base64============================
base64解码:
my $var = decode_base64($1);
my $var = encode_base64($1);
==========================MIME::QuotedPrint======================
可见字符编码:
my $var = decode_qp($1)
my $var = encode_qp($1)
ENCODING=QUOTED-PRINTABLE:=E5=90=B4=E4=B8=9C=E6=96=B0 可还原为汉字
============================Encode============================
utf8编码转换:
my $var = encode("gbk",$var)
my $var = decode("gbk",$var)
$octets = encode("iso-8859-1",$string);把一个串从perl内部格式转为iso-8859-1格式
$string = decode("iso-8859-1",$octets);转换ISO-8859-1数据为perl内部格式串
my $octets = encode(ENCODING,$string [,CHECK])
$string: Perl字符串
encoding: 是给定的编码方式
$octets: 是编码之后的字节流
check: 表示转换时如何处理畸变字符(也就是Perl认不出来的字符)。一般不需使用
eg:
$dat="测试文本";
my $str=decode("utf8",$dat);
@chars=split //,$str;
foreach $char (@chars) {
print encode("utf8",$char),"n";
}
============================Encode============================
编码类型:uft8和utf-8不一样
汉字????????????????? gb2312
perl内部编码?? utf8
error: wide character in print at ./test.pl line 10
有perl不认识的编码,perl一般认识utf8和ascii编码
打开utf8-flag编码:Encode::_utf8_on($str);
eg:
use Encode;
use strict;
my $str = "测试文本";
Encode::_utf8_on($str);
print $str. "n";
Encode::_utf8_off($str);
print $str. "n";
或者:在程序文件的头部加上以下内容
use utf8;
binmode(STDIN,':encoding(utf8)');
binmode(STDOUT,helvetica; font-size:14px; word-wrap:break-word">binmode(STDERR,':encoding(utf8)');