Compilation errors when connecting to MongoDB from Perl

I am trying to run a simple tutorial "MongoDB: Tutorial":

http://search.cpan.org/dist/MongoDB/lib/MongoDB/Tutorial.pod

My goal is to connect to the MongoDB database with a Perl script. I installed MongoDB using cpanm:

$ sudo cpanm MongoDB MongoDB is up to date. (0.501.1) 

I created a simple Perl script called loadRaw.pl :

 use strict; use warnings; use MongoDB; use MongoDB::Connection; use MongoDB::OID; print "hello\n"; 

When I try to run the script, I get a bunch of errors:

 $ perl ./loadRaw.pl String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 271, near "confess "cannot set fields after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 273, near "confess 'not a hash reference'" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 294, near "confess "cannot set sort after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 296, near "confess 'not a hash reference'" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 317, near "confess "cannot set limit after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 343, near "confess "Cannot set tailable state"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 366, near "confess "cannot set skip after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 390, near "confess "cannot set snapshot after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 408, near "confess "cannot set hint after querying"" (Do you need to predeclare confess?) String found where operator expected at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 410, near "confess 'not a hash reference'" (Do you need to predeclare confess?) syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 90, near "has started_iterating" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 271, near "confess "cannot set fields after querying"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 294, near "confess "cannot set sort after querying"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 317, near "confess "cannot set limit after querying"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 343, near "confess "Cannot set tailable state"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 366, near "confess "cannot set skip after querying"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 390, near "confess "cannot set snapshot after querying"" syntax error at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 408, near "confess "cannot set hint after querying"" BEGIN not safe after errors--compilation aborted at /usr/local/lib/perl/5.10.1/MongoDB/Cursor.pm line 564. Compilation failed in require at /usr/local/lib/perl/5.10.1/MongoDB/Connection.pm line 26. BEGIN failed--compilation aborted at /usr/local/lib/perl/5.10.1/MongoDB/Connection.pm line 26. Compilation failed in require at /usr/local/lib/perl/5.10.1/MongoDB.pm line 30. BEGIN failed--compilation aborted at /usr/local/lib/perl/5.10.1/MongoDB.pm line 30. Compilation failed in require at ./loadRaw.pl line 7. BEGIN failed--compilation aborted at ./loadRaw.pl line 7. 

It seems that the MongoDB Perl module (specifically the Cursor.pm file) has some syntax errors. The first batch of problems (related to the confess keyword) is resolved if I add the use Carp; line use Carp; to the beginning of Cursor.pm . However, I do not think I will have to do this, and instead I am doing something else wrong. In addition, the second batch of errors (associated with the has keyword) is not resolved by including Carp .

Has anyone else experienced this? Any ideas?

+4
source share
1 answer

I did not know exactly what was going wrong, but it is certainly unique to my system and my Perl configuration. Since others were able to execute the provided code without problems, I tried to execute the code using Perlbrew (as suggested by friedo):

 $ perlbrew install perl-5.16.0 (Output not shown) $ perlbrew switch perl-5.16.0 (Output not shown) $ perlbrew install-cpanm (Output not shown) $ /home/sthomas/perl5/perlbrew/bin/cpanm MongoDB (Output not shown) $ perlbrew exec perl ./loadRaw.pl perl-5.16.0 ========== hello 

Since it works fine with a Perlbrew installation, the problem is not with MongoDB or my sample code; it must be some weird quirk of my Perl environment. I think I'll try a new install of Perl and all of my required modules and see if this works.

+1
source

Source: https://habr.com/ru/post/1442788/


All Articles