2

I am getting an error when using 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;

The error I get is:

"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."

The entry point is my_action() method of MyApp:Users class.

I tried alternative setups of creating class MyApp::Models::User::Manager : separate .pm file, make_manager_class(), but to no avail.

(I found this discussion from 2007 with the same error message, but it does not help me out http://comments.gmane.org/gmane.comp.lang.perl.modules.dbi.rose-db-object/1537).

This may indicate I am trying to call an object method as if it were a class method. I tried the tricks listed here http://perldoc.perl.org/perlobj.html#Invoking-Class-Methods, but no success.

I now I can examine the contents of variables with Data::Dumper, but I have no clue what to dump as there are very little data structures used.

Mariano
  • 6,423
  • 4
  • 31
  • 47
henq
  • 41
  • 4
  • In case this helps, the next to immediate cause of the error is that Rose::DB::Object's `db` method was called as a static method (`Rose::DB::Object->db` or `$class->db`) instead of an instance method (`$object->db`). You might want to use [Carp::Always](http://search.cpan.org/perldoc?Carp::Always) to see how you got there. – ikegami Sep 23 '15 at 02:06
  • You keep doing stuff like in validate: `my $u = shift; MyApp::Models::User::Manager::->get_users_count` - if you're going to make an object and inherit, why throw it away? why not `$u->get_users_count`? – bytepusher Sep 23 '15 at 08:35
  • I was searching in the wrong location, with help your Carp::Always hint @ikegami I noticed it, thank you. As my error is not very interesting for others I will delete this post in a short while. – henq Sep 24 '15 at 21:48
  • @bytepusher , the $u is left over code, I stripped many lines of code away for this post. The get_users_count() function must indeed be called as a class method. – henq Sep 24 '15 at 21:53
  • ah, sorry for the confusion. It did look strange, but had nothing to do with anything apparently. – bytepusher Sep 25 '15 at 10:50

1 Answers1

0

While use strict is a good idea when writing Perl code, you may want to relax the strict-ness by adding

no strict `refs`;

to get past the current error. As @ikegami pointed out another way to fix this is to get rid of the bad reference, but if you don't want to rewrite the module working around it with relaxing strict-ness is your best bet.

chicks
  • 2,393
  • 3
  • 24
  • 40