简单的 Perl 脚本(Perl 菜鸟,刚刚开始)
我不在学校或其他任何地方,但我有一个我想完成的示例测试。我不是 100% 确定从哪里开始(正如我所说,我对 Perl 完全陌生,但真的非常想进入它)。
给定一个表“mailing”:
CREATE TABLE mailing (
addr VARCHAR(255) NOT NULL
);
邮件表最初是空的。每天都会添加新地址。预计该表将存储至少 10,000,000 个电子邮件地址和 100,000 个域名。
编写一个 perl 脚本来更新另一个表,该表按域名保存电子邮件地址的每日计数。
使用此表可报告按计数排名前 50 个的域(按过去 30 天与总数相比的增长百分比排序)。
注意 - 您必须使用提供的DB.pm
进行所有数据库交互,并且必须按原样使用它(除了连接设置之外,不能修改DB.pm)。
不应修改原始邮件表。
所有处理必须在 Perl 中完成(例如,没有复杂查询或子查询)
这是 DB.pm
package GUI::DB;
use strict;
use DBI;
use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(dbConnect query);
#
# dbConnect - connect to the database, get the database handle
#
sub dbConnect {
# Read database settings from config file:
my $dsn = "DBI:mysql:database=test";
my $dbh = DBI->connect( $dsn,
'',
'',
{ RaiseError => 1 }
);
return $dbh;
}
#
# query - execute a query with parameters
# query($dbh, $sql, @bindValues)
#
sub query {
my $dbh = shift;
my $sql = shift;
my @bindValues = @_; # 0 or several parameters
my @returnData = ();
# issue query
my $sth = $dbh->prepare($sql);
if ( @bindValues ) {
$sth->execute(@bindValues);
} else {
$sth->execute();
}
if ( $sql =~ m/^select/i ) {
while ( my $row = $sth->fetchrow_hashref ) {
push @returnData, $row;
}
}
# finish the sql statement
$sth->finish();
return @returnData;
}
__END__
我经常使用 PHP 和类似的有趣的东西,但 Perl 就适合我。
I'm not in school or anything, but I have this example test that I want to complete. I'm not 100% sure where to begin (as said, I am completely new to Perl but really, really want to get into it).
Given a table 'mailing':
CREATE TABLE mailing (
addr VARCHAR(255) NOT NULL
);
The mailing table will initially be empty. New addresses will be added on a daily basis. It is expected that the table will store at least 10,000,000 email addresses and 100,000 domains.
Write a perl script that updates another table which holds a daily count of email addresses by their domain name.
Use this table to report the top 50 domains by count sorted by percentage growth of the last 30 days compared to the total.
NOTE
- You MUST use the provided DB.pm
for all database interaction, and you must use it as it is (DB.pm cannot be modified except for the connection settings).
The original mailing table should not be modified.
All processing must be done in Perl (eg. no complex queries or sub-queries)
And here is the DB.pm
package GUI::DB;
use strict;
use DBI;
use vars qw(@ISA @EXPORT);
use Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(dbConnect query);
#
# dbConnect - connect to the database, get the database handle
#
sub dbConnect {
# Read database settings from config file:
my $dsn = "DBI:mysql:database=test";
my $dbh = DBI->connect( $dsn,
'',
'',
{ RaiseError => 1 }
);
return $dbh;
}
#
# query - execute a query with parameters
# query($dbh, $sql, @bindValues)
#
sub query {
my $dbh = shift;
my $sql = shift;
my @bindValues = @_; # 0 or several parameters
my @returnData = ();
# issue query
my $sth = $dbh->prepare($sql);
if ( @bindValues ) {
$sth->execute(@bindValues);
} else {
$sth->execute();
}
if ( $sql =~ m/^select/i ) {
while ( my $row = $sth->fetchrow_hashref ) {
push @returnData, $row;
}
}
# finish the sql statement
$sth->finish();
return @returnData;
}
__END__
I work with PHP and fun stuff like that regularly, but Perl is just out there for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是 Perl
DBI
用法的示例,它将记录插入到mailing
表中:Here is an example of Perl
DBI
usage that inserts a record intomailing
table:鉴于规则规定您必须使用
DB.pm
中的query
方法,那么:DB.pm
模块有缺陷,因为它不提供错误返回指示 - 它依赖于 DBI RaiseError 来生成错误。它也不提供终止数据库连接的方法。DB.pm 中的代码很不稳定。
dbConnect()
方法是:它可能是:
这是 6 行而不是 13 行,而且实际上比原来的更容易阅读。
Given that the rules state that you must use the
query
method fromDB.pm
, then:The
DB.pm
module is defective in that it does not provide a error return indication - it relies on the DBI RaiseError to generate the error. It also does not provide a way to terminate the database connection.The code in DB.pm is flabby. The
dbConnect()
method is:It could be:
That's 6 lines instead of 13, and is actually easier to read than the original.