4

I'm trying to run the simple "MongoDB:Tutorial" tutorial:

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

My goal is to connect to a MongoDB database from a Perl script. I've 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 like the MongoDB Perl module (specifically, the Cursor.pm file) has some syntax errors. The first batch of problems (those related to the confess keyword) are solved if I add the line use Carp; to the top of Cursor.pm. However, I don't think I should have to do this, and instead I'm doing something else incorrectly. Also, the second batch of errors (those related to the has keyword) are not solved by including Carp.

Has anyone else experienced this? Any ideas?

4

1 回答 1

1

我从来没有确切地知道出了什么问题,但它肯定是我的系统和我的 Perl 配置所独有的。由于其他人能够毫无问题地执行提供的代码,我尝试使用 Perlbrew 执行代码(如frido所建议的那样):

$ 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

因为它在 Perlbrew 安装中运行良好,所以问题不在于 MongoDB 或我的示例代码;这一定是我的 Perl 环境的一些奇怪的怪癖。我想我会尝试全新安装 Perl 和所有我需要的模块,看看是否可行。

于 2012-10-29T23:50:38.053 回答