开发者

Simple Perl Script (Total Perl Noob, just starting out) [closed]

开发者 https://www.devze.com 2023-03-09 12:16 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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 wil开发者_开发百科l 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.


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(?)', 'test@test.com');
$dbh->disconnect();


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(?)", 'someone@example.com');

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号