Perl object error: cannot find object method through package

I understand that there are several such questions on the air, but I can’t solve the problem. Perhaps I should improve my lateral thinking.

I have a module that I am testing. This module looks something like this:

package MyModule; use strict; use warnings; ... # a bunch of 'use/use lib' etc. sub new { my $class = shift; my ($name,$options) = @_; my $self = { _name => $name, _features => $options, _ids => undef, _groups => undef, _status => undef, }; bless $self,$class; return $self; } sub init { my ($self) = @_; my ($ids,$groups,$status) = ...; # these are from a working module $self->{_ids} = $ids; $self->{_groups} = $groups; $self->{_status} = $status; return $self; } 

This is my test file:

 #!/usr/bin/perl -w use strict; use MyModule; use Test::More tests => 1; use Data::Dumper; print "Name: "; my $name; chomp($name = <STDIN>); print "chosen name: $name\n"; my %options = ( option1 => 'blah blah blah', option2 => 'blu blu blu', ); my $name_object = MyModule->new($name,\%options); print Dumper($name_object); isa_ok($name_object,'MyModule'); $name_object->init; print Dumper($name_object); 

Now it works before isa_ok , but then it appears:

Can't locate object method "init" via package "MyModule" at test_MyModule.t line 31, <STDIN> line 1.

This happened only now, when I try (and somewhat fail) to use objects. Therefore, therefore, I believe that I misunderstand the application of objects in Perl! Any help would be appreciated ...

+6
source share
1 answer

I think that you are loading a different file than the one you think is loading.

 print($INC{"MyModule.pm"}, "\n"); 

will tell you which file you downloaded. (If the module name really looks like Foo::Bar , use $INC{"Foo/Bar.pm"} .) Make sure that the capital letter package and the file name match.

+7
source

All Articles