0

My app crashes each time it includes as much as "use MongoDB;" in my perl app file.

I have installed MongoDB successfully. I can check my databases use one or the other, check for collections, create new collections, all from the shell.

If I try to connect to mongoDb from mojolicious app like:

!/usr/bin/env perl

use Mojolicious::Lite;
use MongoDB;
use MongoDB::OID;

my $mongo_port = shift || 27017;

helper 'mongo' => sub {
    my ($self, $name) = @_;
    my $host = 'localhost:' . $mongo_port;
    my $conn = MongoDB::MongoClient->new(host => $host);
    my $db = $conn->get_database('test');
};

helper 'value2oid' => sub {
    my ($self, $value) = @_;
    MongoDB::OID->new($value);
};

If I have a working app and include as much as :

Use MongoDB;

I get:

Can't load application from file "/Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl": Can't locate MongoDB.pm in @INC (you may need to install the MongoDB module) (@INC contains: /Library/Perl/5.18/darwin-thread-multi-2level /Library/Perl/5.18 /Network/Library/Perl/5.18/darwin-thread-multi-2level /Network/Library/Perl/5.18 /Library/Perl/Updates/5.18.2/darwin-thread-multi-2level /Library/Perl/Updates/5.18.2 /System/Library/Perl/5.18/darwin-thread-multi-2level /System/Library/Perl/5.18 /System/Library/Perl/Extras/5.18/darwin-thread-multi-2level /System/Library/Perl/Extras/5.18 .) at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.
BEGIN failed--compilation aborted at /Users/eevitomperi/Desktop/Programming/Perl/mojoliciousApp/foodAbout/app.pl line 4.

I am completely new to mongo, mojolicious and perl so I guess I did not install some package?

Does the MongoDB files(mongo, mongod....) have to be within the mojolicious project ?

Not sure what I am missing and all documentation starts with the use of "Use MongoDB;" within mojolicious app so not sure what to do.

Hopefully someone can point out what I missed.

Al Ex Tsm
  • 2,042
  • 2
  • 29
  • 47
  • Did you install the module? Probably not You are probalbly more interested in [Mogolicious::Plugin::Mongodb](http://search.cpan.org/~madcat/Mojolicious-Plugin-Mongodb-1.16/lib/Mojolicious/Plugin/Mongodb.pm) anyhow, given your stack choice. Remember to actually install from CPAN this time. – Neil Lunn Mar 11 '15 at 10:53
  • @ Neil Lunn Thank you. I have downloaded "Mojolicious-Plugin-Mongodb-1.16" from cpan.org. Could u tell me where I have to store it within my mojolicious project or where I can document myself better. – Al Ex Tsm Mar 11 '15 at 10:58
  • Way too broad. CPAN installs are either "global" or localized depending on use of something like [perlbrew](http://perlbrew.pl/) or [plenv](https://github.com/tokuhirom/plenv) or even [local::lib](https://metacpan.org/pod/local::lib). Not trying to be condescending, but your "downloaded" comment does not instill faith that you have actually done any of these things correctly. – Neil Lunn Mar 11 '15 at 11:09
  • Thanks for the comments @ Neil Lunn – Al Ex Tsm Mar 11 '15 at 12:35

1 Answers1

1

Install module:

cpanm Mojolicious::Plugin::Mongodb

Fix the following:

Can't write to /Library/Perl/5.18 and /usr/local/bin: Installing modules to /Users/eevitomperi/perl5
! To turn off this warning, you have to do one of the following:
!   - run me as a root or with --sudo option (to install to /Library/Perl/5.18 and /usr/local/bin)
!   - Configure local::lib your existing local::lib in this shell to set PERL_MM_OPT etc.
!   - Install local::lib by running the following commands

By running:

cpanm --local-lib=~/perl5 local::lib && eval $(perl -I ~/perl5/lib/perl5/ -Mlocal::lib)

Now I can connect to Mongo from mojolicious

Al Ex Tsm
  • 2,042
  • 2
  • 29
  • 47