Perl Rose :: DB中的错误:不能使用字符串...作为HASH引用而“严格”

时间:2015-09-22 22:12:47

标签: perl rose-db

使用Rose :: DB时出错。

#MyApp/DB.pm
package MyIMDB::DB;
use strict;  use warnings;
use base qw(Rose::DB);
__PACKAGE__->use_private_registry;
__PACKAGE__->register_db (
    driver   => 'SQLite',
    ....
);
1; 

# MyApp/DB/Object.pm
package MyApp::DB::Object;
use strict;  use warnings;
use MyApp::DB;
use base qw(Rose::DB::Object);
sub init_db { MyIMDB::DB->new }
1;

#
package MyApp::Users;   #controller       
use strict;  use warnings;
use base 'Mojolicious::Controller';
use Mojo::ByteStream 'b';
use MyApp::Models::User;
use Data::Dumper;

sub my_action {
  my $uc = shift;
  my $err =  MyApp::Models::User::->validate(...);   #extra ::
                            # http://perldoc.perl.org/perlobj.html#Invoking-Class-Methods
}

# MyApp/Models/User.pm    # 2 packages in this file
package MyApp::Models::User::Manager;
use base qw(Rose::DB::Object::Manager);
use MyApp::Models::User;
sub object_class { 'MyApp::Models::User'}
__PACKAGE__->make_manager_methods('users');
  # class methods get_x, get_x_iterator, get_x_count, delete_x, update_x
1;

MyApp::Models::User
use strict;  use warnings;
use base qw(MyApp::DB::Object);
__PACKAGE__->meta->setup(
    #setup tables, columns....
  );

sub validate {
  my $u = shift;
  my $n = MyApp::Models::User::Manager::->get_users_count(query => [user_name => $user]);
}  
1;

我得到的错误是:

"Can't use string ("MyApp::Models::User") as a HASH ref while "strict refs" 
 in use at /usr/local/share/perl/5.18.2/Rose/DB/Object.pm line 91, <DATA> line 2231."

入口点是my_action()类的MyApp:Users方法。

我尝试了创建类MyApp::Models::User::Manager的其他设置:单独的.pm文件,make_manager_class(),但无济于事。

(我在2007年的讨论中发现了相同的错误消息,但它并没有帮助我http://comments.gmane.org/gmane.comp.lang.perl.modules.dbi.rose-db-object/1537)。

这可能表明我正在尝试将对象方法称为类方法。我尝试了http://perldoc.perl.org/perlobj.html#Invoking-Class-Methods中列出的技巧,但没有成功。

我现在可以使用Data::Dumper检查变量的内容,但我不知道要转储什么,因为使用的数据结构非常少。

1 个答案:

答案 0 :(得分:0)

虽然在编写Perl代码时use strict是个好主意,但您可能希望通过添加

来放宽严格性
no strict `refs`;

超越当前错误。正如@ikegami所指出的另一种方法是解决这个错误的引用,但是如果你不想重写模块,那么放松严格是最好的选择。