Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

MongoDB Perl Wrapper

by IruP (Initiate)
on Sep 07, 2017 at 07:57 UTC ( #1198825=perlquestion: print w/replies, xml ) Need Help??

IruP has asked for the wisdom of the Perl Monks concerning the following question:

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 are:
1. Would you create the module different?
2. Do you have improvements or adaptations to create the module better?

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;

Replies are listed 'Best First'.
Re: MongoDB Perl Wrapper
by hippo (Canon) on Sep 07, 2017 at 08:25 UTC
    use Exporter; #... @ISA = qw(Exporter); @EXPORT = qw(); @EXPORT_OK = qw(); %EXPORT_TAGS = ( DEFAULT => [qw()], Both => [qw()]);

    I fail to see the point of making your module load and inherit from Exporter if you aren't going to have it export anything. Can you explain this?

    The rest of it seems fine at first glance but I'm no MongoDB expert.

    Update: Turns out this question was crossposted at stackexchange and as such is likely to be another of IruP's plagiarism attempts.

Re: MongoDB Perl Wrapper
by Corion (Pope) on Sep 07, 2017 at 08:37 UTC

    Code-wise, I would make ->_set_connection respect the ->{port} and ->{host} arguments.

    Also, I found a cascade of arguments makes testing and overriding things much easier in such routines. I would take the URL from the passed arguments, or from $self, in that order:

    sub _set_connection { my( $self, %options ) = @_; my $url = $options{ url }; $url ||= sprintf 'http://%s:%s', $self->{host}, $self->{port}; return MongoDB->connect($url); }
Re: MongoDB Perl Wrapper
by roboticus (Chancellor) on Sep 07, 2017 at 16:03 UTC


    I'd add:

    • Error Checking: You're currently assuming that some parameters are passed, but not enforcing it. So when an error occurs, you don't know what's going to happen. Any changes to the MongoDB class won't be reflected in your own.
    • A test suite: You ought to verify that your library works as intended so you don't get surprised.
    • Make it do something: Currently it doesn't provide any functionality not already provided by MongoDB, so what's the point of it? It renames a couple of functions, but that would only make it a little harder to use, since users of MongoDB would expect different names.

    As a learning exercise, it seems like a decent start.


    When your only tool is a hammer, all problems look like your thumb.

Re: MongoDB Perl Wrapper
by Anonymous Monk on Sep 08, 2017 at 21:18 UTC
    Hello, did you have written the code or did you have the part of code from my post ;) ?
    In May i wrote this question

    You had copied the hole text and code.
    I have no problem, if you use or improve this code.
    But please don't sell it as your code ;)

    This MongoWrapper is also under my github account.
    If someone want to improve this, you are welcome :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1198825]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2019-05-24 03:41 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (147 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!