用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - perl代码库

perl 建立和使用dbm文件

2012-10-14 作者: 神马举报

[perl]代码库

#-----------------------------
use DB_File;
# optional; overrides default
dbmopen %HASH, $FILENAME, 0666    # open database, accessed through %HASH
or die "Can't open $FILENAME: $!\n";

$V = $HASH {$KEY};
# retrieve from database
$HASH {$KEY} = $VALUE;
# put value into database
if ( exists $HASH {$KEY} )
{
# check whether in database
# ...
}
delete $HASH {$KEY};
# remove from database
dbmclose %HASH;
# close the database
#-----------------------------
use DB_File;
# load database module

tie %HASH, "DB_File", $FILENAME   # open database, to be accessed
or die "Can't open $FILENAME:$!\n";
# through %HASH

$V = $HASH {$KEY};
# retrieve from database
$HASH {$KEY} = $VALUE;
# put value into database
if ( exists $HASH {$KEY} )
{
# check whether in database
# ...
}
delete $HASH {$KEY};
# delete from database
untie %hash;
# close the database
#-----------------------------
# download the following standalone program
#!/usr/bin/perl -w
# userstats - generates statistics on who is logged in.
# call with an argument to display totals

use DB_File;

$db = '/tmp/userstats.db';
# where data is kept between runs

tie ( %db, 'DB_File', $db )         or die "Can't open DB_File $db : $!\n";

if ( @ARGV )
{
	if ( "@ARGV" eq "ALL" )
	{
		@ARGV = sort keys %db;
	}
	foreach $user ( @ARGV )
	{
		print "$user\t$db{$user}\n";
	}
}
else
{
	@who = `who`;
# run who(1)
	if ( $? )
	{
		die "Couldn't run who: $?\n";
# exited abnormally
	}
# extract username (first thing on the line) and update
	foreach $line ( @who )
	{
		$line =~ /^ ( \S+ ) /;
		die "Bad line from who: $line\n" unless $1;
		$db {$1}++;
	}
}

untie %db;

#-----------------------------
gnat     ttyp1   May 29 15:39   ( coprolith.frii.com )
#-----------------------------


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...