[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 )
#-----------------------------