简单的 Perl 脚本(Perl 菜鸟,刚刚开始)

发布于 2024-11-09 23:11:36 字数 1845 浏览 2 评论 0原文

我不在学校或其他任何地方,但我有一个我想完成的示例测试。我不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

避讳 2024-11-16 23:11:36

以下是 Perl DBI 用法的示例,它将记录插入到 mailing 表中:

use DBI;
$dbh = DBI->connect('DBI:mysql:databasename', 'username', 'password'
               ) || die "Could not connect to database: $DBI::errstr";
$dbh->do('INSERT INTO mailing VALUES(?)', '[email protected]');
$dbh->disconnect();

Here is an example of Perl DBI usage that inserts a record into mailing table:

use DBI;
$dbh = DBI->connect('DBI:mysql:databasename', 'username', 'password'
               ) || die "Could not connect to database: $DBI::errstr";
$dbh->do('INSERT INTO mailing VALUES(?)', '[email protected]');
$dbh->disconnect();
本王不退位尔等都是臣 2024-11-16 23:11:36

鉴于规则规定您必须使用 DB.pm 中的 query 方法,那么:

my $dbh = dbConnect() or die "A horrible death";
query($dbh, "INSERT INTO Mailing(addr) VALUES(?)", '[email protected]');

DB.pm 模块有缺陷,因为它不提供错误返回指示 - 它依赖于 DBI RaiseError 来生成错误。它也不提供终止数据库连接的方法。


DB.pm 中的代码很不稳定。 dbConnect() 方法是:

sub dbConnect {

        # Read database settings from config file:
        my $dsn = "DBI:mysql:database=test";
        my $dbh = DBI->connect( $dsn,
                '',
                '',
                                { RaiseError => 1 } 
    );

        return $dbh;

}

它可能是:

sub dbConnect
{
        # Read database settings from config file:
        my $dsn = "DBI:mysql:database=test";
        return DBI->connect($dsn, '', '', { RaiseError => 1 });
}

这是 6 行而不是 13 行,而且实际上比原来的更容易阅读。

Given that the rules state that you must use the query method from DB.pm, then:

my $dbh = dbConnect() or die "A horrible death";
query($dbh, "INSERT INTO Mailing(addr) VALUES(?)", '[email protected]');

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:

sub dbConnect {

        # Read database settings from config file:
        my $dsn = "DBI:mysql:database=test";
        my $dbh = DBI->connect( $dsn,
                '',
                '',
                                { RaiseError => 1 } 
    );

        return $dbh;

}

It could be:

sub dbConnect
{
        # Read database settings from config file:
        my $dsn = "DBI:mysql:database=test";
        return DBI->connect($dsn, '', '', { RaiseError => 1 });
}

That's 6 lines instead of 13, and is actually easier to read than the original.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文