connect, use, reference, and then, die, what about the mysql handler? Hang…
then, more and more mysql connect untill you can’t connect anymore….
in perl, you can use module’s DESTROY function to aotu close the dbh( the save when file handle).
ex.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| package HiConnectDB;
use DBI;
sub new {
my $class = shift;
my ($dbname, $dbuser, $dbpass) = @_;
$class = ref($class) || $class;
my $self = bless {}, $class;
$self->{'_dbh'} = DBI->connect("dbi:mysql:database=$dbname;host=localhost",
$dbuser, $dbpass)
or return undef;
return $self;
}
sub DESTROY {
shift()->{_dbh}->disconnect;
$self->SUPER::DESTROY if $self->can("SUPER::DESTROY");
}
1; |
在主程序中使用上面的模块:
1
2
3
4
5
6
7
8
9
10
11
12
13
| sub myfun {
use HiConnectDB;
my $db = new HiConnect("dbname", "dbuser", "dbpass");
or die "Can't connect to db with (dbname, dbuser, xxxx)\n";
$dbh = $db->{_dbh};
$dbh->do("show tables")
### barbarbar, other work
### barbarbar, other work
### barbarbar, other work
### befor exit this route, $db is destroied, and dbh is closed.
} |
at here:
http://www.onlamp.com/pub/a/onlamp/2008/02/19/developing-restful-web-services-in-perl.html
perl想到的,几乎都可以实现(当然, 其它语言也可以实现, 只是方便程序不同而已).
还有几乎无所不包的cpan
在C中, 当然可以调用C代码, 包括内嵌的C语句, 或C/C++写的动态链接库.
调用方面用的多的有两个: XS和Inline::c ,这里使用了inline::c
嵌入动态链接库:
例如我有一个运态链接库名字为libmylib.so,放在默认的目录下, 里面有个函数CheckDomain(in, out), 第一个参数为输入参数, 第二个参数是返回参数, 我们可以写一个wapper,然后像下面这样调用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| #!/usr/bin/perl -w
use Inline C => Config => LIBS => '-lmylib';
use Inline C;
use strict;
$| = 1;
my $ip = '10.10.10.10';
my $domain = ';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;';
my $ret = getDomain($ip, $domain);
if($ret == 0)
{
$domain =~ s/;+//g;
print $domain, "\n";
} else
{
print "error\n";
exit 1;
}
__END__
__C__
int getDomain(char *in, char *out)
{
return CheckDomain(in, out);
} |
如果不需要动态链接库, 直接嵌入C语句, 就更简单一些, 在下面直接写代码, 在上面调用即可。
注: 为何上面的$domain变量写了许多没用的分号?因为可能是C和perl的空间分配不同, C中是靠代码中自己控制的, 而perl中编码人员不需要关心。 这两个的差异会造成一个问题:perl中你分配置了2字节, 则在c中返回的参数中也只能返回2字节(会不会溢出?呀呀呀)。故在perl中多分配了一些长度。
[ad]
lustre的manual上有个用shell写的脚本, 但那个脚本需要等查找整个目录完了才开始重写.如果目录很大(如果不大就不需要做集群了), 这个搜索的时间会非常长. 我用perl重新实现了这个脚本, 可以边搜索边重写.
lustre难免某个ost因为空间变少或其它原因, 要更换ost. 这时, 就需要把这个ost上的数据导出转移到其它ost上,然后再更换设备.
或者, 当新加了一个ost, 但这里面没有数据, 而同时其它的ost上数据过多, 这时负载不均.
这个脚本用来把某个目录中属于某个OST上的文件, 重写来达到均衡.如果你是需要迁移数据, 请先把指定的ost deactivate.( lctl dl; lctl –device n deactivate)
注: 本脚本未保留原文件的属主, 权限位等属性. 如对此有要求, 请暂时用系统的cp命令并加-dp选项.
1 #!/usr/bin/perl -w
2
3 use Digest::MD5;
4 use File::Copy;
5 use File::Temp qw/:mktemp/;;
6 use strict;
7
8
9 if (@ARGV != 2)
10 {
11 print “Usage: <obd> <directory>\n”;
12 exit 1;
13 }
14
15 open IN, ‘-|’, “lfs getstripe -r -O $ARGV[0] $ARGV[1]” or die “$!”;
16 #open IN, “<felix” or die “$!”;
17
18 while (<IN>)
19 {
20 chomp;
21 my $f = $_;
22 if (-d $f)
23 {
24 print “$f : is a directory, skip\n”;
25 next;
26 }
27
28 ### get the md5
29 my $md5 = Digest::MD5->new;
30 open F, $f or die “Can’t read $f: $!”;
31 binmode(F);
32 $md5->addfile(*F);
33 close F;
34
35 ### copy to temporary file
36 my $tpl = $f . ‘.XXXXXX’;
37 my $new = mktemp($tpl);
38 unless(copy($f, $new))
39 {
40 print “$f: copy error: $!”;
41 unlink $new;
42 exit 1;
43 }
44
45 my $newmd5 = Digest::MD5->new;
46 open NF, $new or die “Can’t read $new: $!”;
47 binmode(NF);
48 $newmd5->addfile(*NF);
49 close NF;
50 if( $md5->hexdigest ne $newmd5->hexdigest)
51 {
52 print “$new bad checksum, $f not moved\n”;
53 unlink $new;
54 exit 1;
55 }
56 unless (move($new, $f))
57 {
58 print “$f move error: $!”;
59 unlink $new;
60 exit 1;
61 }
62 print “$f\n”;
63 }