\$\begingroup\$
\$\endgroup\$
5
I'm trying to create a custom "mongodb perl wrapper". I created the following perl module, which local on my machine runs. My questions to the community are:
- Would you create the module different?
- Do you have improvements or adaptations to create the module better?
Code:
package MyMongo;
use strict;
use Exporter;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
use MongoDB;
$VERSION = 1.00;
@ISA = qw(Exporter);
@EXPORT = qw();
@EXPORT_OK = qw();
%EXPORT_TAGS = ( DEFAULT => [qw()],
Both => [qw()]);
sub new {
my ($class, $args) = @_;
my $self = {
host => $args->{host} || 'localhost',
port => $args->{port} || '27017',
database => $args->{database} || '',
connection => $class->_set_connection(),
};
return bless $self, $class;
}
sub _set_connection {
my $self = shift;
my $client = MongoDB->connect('mongodb://localhost');
return $client;
}
sub get_database_names {
# Lists all databases on the mongo server
my $self = shift;
return $self->{connection}->database_names;
}
sub get_database {
# Returns a MongoDB::Database instance for database with the given $name
my ($self, $dbname) = @_;
return $self->{connection}->get_database($dbname);
}
sub authenticate {
my ($self, $args) = @_;
$self = {
dbname => $args->{dbname},
user => $args->{user},
password => $args->{password}
};
$self->{connection}->authenticate($self->{dbname}, $self->{user}, $self->{password});
}
1;
Ludisposed
11.8k2 gold badges41 silver badges91 bronze badges
asked May 18, 2017 at 6:10
-
1\$\begingroup\$ Plagiarised here: perlmonks.org/?node_id=1198825 \$\endgroup\$choroba– choroba2017年09月07日 18:32:06 +00:00Commented Sep 7, 2017 at 18:32
-
\$\begingroup\$ Thanks @choroba for the hint. Yes your are right, the guy has stolen my part of code i had wrote. \$\endgroup\$Patrick85– Patrick852017年09月08日 21:13:53 +00:00Commented Sep 8, 2017 at 21:13
-
\$\begingroup\$ @Patrick85 May I ask why did you delete this? \$\endgroup\$Ludisposed– Ludisposed2017年12月22日 10:27:21 +00:00Commented Dec 22, 2017 at 10:27
-
\$\begingroup\$ The process for asking for deletion of an answered question is described in meta.stackexchange.com/a/5222/213556 \$\endgroup\$Simon Forsberg– Simon Forsberg2017年12月22日 11:29:01 +00:00Commented Dec 22, 2017 at 11:29
-
\$\begingroup\$ There is an intention that people may come across this years in the future, and so you aren't always allowed to delete your posts (without reasoning with a moderator) or allowed to deface the website by editing your question such as you have done. Basically while you may not need the information here, leaving it may still be a benefit for others. \$\endgroup\$Brad Gilbert– Brad Gilbert2017年12月22日 14:52:14 +00:00Commented Dec 22, 2017 at 14:52
2 Answers 2
\$\begingroup\$
\$\endgroup\$
1
I'm the lead engineer for the MongoDB Perl driver with a couple of thoughts for you:
- you're using 'authenticate', which is for a very old version of the driver which is not recommended for use. In the v1.x series, you should provide username/password in the URI or in MongoClient parameters.
- Most of the value of this module seems to be setting parameters and calling
connect
. I would suggest doing that with a function, not a module and then just using the resulting MongoDB::MongoClient object directly.
-
\$\begingroup\$ Thanks for your hint. I will change my wrapper to work with MongoDB::MongoClient. After Iam ready i will show erverybody my wrapper. Thanks \$\endgroup\$Patrick85– Patrick852017年05月18日 14:11:38 +00:00Commented May 18, 2017 at 14:11
\$\begingroup\$
\$\endgroup\$
Please check comments in the code,
# package using block, convenient but not mandatory
package MyMongo {
use strict;
# as it seems you just need import() from Exporter module
use Exporter 'import';
use MongoDB;
# use vars qw(..) works for older perl, but since 5.6 "our" is preferred
our $VERSION = 1.00;
# it seems you don't actually want to be child of Exporter, so no parent class
our @ISA = qw();
our @EXPORT = qw();
our @EXPORT_OK = qw();
our %EXPORT_TAGS = ( DEFAULT => [qw()],
Both => [qw()]);
sub new {
my ($class, $args) = @_;
my %defaults = (
host => 'localhost',
port => '27017',
database => '',
);
my $self = bless({ %defaults, %$args }, $class);
$self->{connection} = $self->_set_connection();
return $self;
}
# ...
}
1;
More suggestions in perldoc Exporter
answered May 18, 2017 at 10:13
lang-perl