Class::DBI 是使简单的数据库操作几乎不用编程,同时使困难的变得有可能。对很多简单的数据库应用来说,它使我们完全不用编 写SQL,另一方面它也不强迫你用很复杂的数据结构来表示一个复杂查询。如果你确实需要原始SQL的功能或表达能力,它也会适时的给你让路。
例子程序:
#!/usr/bin/perl use My::PhoneBill::Call; my $number = shift or die "Usage: $0 <number>"; my @calls = My::PhoneBill::Call->search(number => $number); my $total_cost = 0; foreach my $call (@calls) { $total_cost += $call->cost; printf "%s %s - %d secs, %.1f pence\n", $call->calldate, $call->calltime, $call->duration, $call->cost; } printf "Total: %d calls, %d pence\n", scalar @calls, $total_cost;