0
votes
BaseClass.pm

package Test::Base::BaseClass;

sub new {

    return bless {hello=>@_[0],person=>@_[1]} , __PACKAGE__;    
}

sub hello {
    print "hello";
}

sub person {
    my $self = shift;
    return $self->{person};
}

1;

Sub.pm
package Test::Base::BaseClass;


sub sub_ {

    my $self= shift;
    print __PACKAGE__;

}

1;

example.pl

use lib 'C:/Users/pavan.t/workspace/Simple';
use Test::Base::BaseClass;
$sub =  Test::Base::BaseClass->new('pavan','pavan');

print $sub->person;
print $sub->sub_

I have one BaseClass package and another module Sub.pm which belongs to same package.

In my example program, when i call the sub_ method, it prints the following error as:

Can't locate object method "sub_" via package "Test::Base::BaseClass" at C:/Users/pavan.t/workspace/Simple/ExampleOnBase.pl line 12.

1
That should be sub new { return bless {hello=>@_[1],person=>@_[2]}, $_[0]; } - Brad Gilbert

1 Answers

1
votes

If you use a package Foo::Bar::Baz, perl is searching for a file foo/bar/baz.pm somewhere in the INC path. Your script uses Test::Base::BaseClass so it finds BaseClass.pm. There's no sub sub_ defined.

You should be able to add a use Sub, no matter which package is defined in there.

However, packages and file names should (they don't have to but it's better!) be similar.