6
\$\begingroup\$

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;
\$\endgroup\$
  • 1
    \$\begingroup\$ Plagiarised here: perlmonks.org/?node_id=1198825 \$\endgroup\$ – choroba Sep 7 '17 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 Sep 8 '17 at 21:13
  • \$\begingroup\$ @Patrick85 May I ask why did you delete this? \$\endgroup\$ – Ludisposed Dec 22 '17 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 Dec 22 '17 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 Dec 22 '17 at 14:52
7
\$\begingroup\$

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.
\$\endgroup\$
  • \$\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 May 18 '17 at 14:11
3
\$\begingroup\$

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.