http://www.perlmonks.org?node_id=1047553

perl_noobie has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm new to OO perl and encountering the "Can't locate object method" issue. I have 2 classes with 2 subclasses under each of them - Person - Baby - Adult - Employee - IT - Finance I'm creating an obj for the Employee Subclass and based on the age of the employee, i'm loading a subclass from person. But i'm not able to call any method from the Person subclass. Posted my code below. Can someone please help? PS: Please note that each package is an individual file

package Person; use strict; use warnings; sub new { my $class = shift; my $self1 = { NAME => "erere", AGE => 233, @_, }; my $pkg; my $age = $self1->{AGE}; if ($age < 20) { $pkg = "Person::Baby"; } else { $pkg = "Person::Adult"; } eval "require $pkg"; my $function_name = $pkg . "::new"; my $func_ref = \&$function_name; my $self = $func_ref->($class, %$self1); return bless $self, $pkg; } sub sayHello { print "Hello from some Person\n"; } 1;
package Person::Adult; use strict; use warnings; use Exporter; use base ("Person"); use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS); @EXPORT = qw( sayHi ); sub new { my $class = shift; my $self = { NAME => "erere", AGE => 233, @_, }; bless($self, "Adult"); return $self; } sub sayHi { my $self = shift; print "######## Adult says Hi !!!!!!!\n"; return; } 1; package Person::Baby; use strict; use warnings; use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS); @EXPORT = qw( sayHi ); use base ("Person"); use Exporter; sub new { my $class = shift; my $self = { NAME => "erere", AGE => 233, @_, }; bless($self, "Baby"); return $self; } sub sayHi { my $self = shift; print "######## Baby says Hi !!!!!!!\n"; } 1; package Employee; use strict; use warnings; use base ("Person"); sub new { my $class = shift; my $self1 = { NAME => "nghnh", AGE => 23, @_, }; my $pkg; my $dept = $self1->{DEPT}; if ($dept =~ /IT/i) { $pkg = "Employee::IT"; } else { $pkg = "Employee::Finance"; } eval "require $pkg"; my $function_name = $pkg . "::new"; my $func_ref = \&$function_name; my $self = $func_ref->($class, %$self1); return bless $self, $pkg; } 1; package Employee::IT; use strict; use warnings; use base ("Employee", "Person"); sub new { my $class = shift; my $self = { NAME => "erere", DEPT => "IT", AGE => 23, @_, }; bless($self, "IT"); my $age = $self->{AGE}; if ($age < 20) { eval "require Person::Baby"; import Person::Baby; } else { eval "require Person::Adult"; import Person::Adult; } return $self; } 1; package Employee::Finance; use strict; use warnings; use base ("Employee", "Person"); sub new { my $class = shift; my $self = { NAME => "erere", DEPT => "Finance", AGE => 23, @_, }; bless($self, "Finance"); my $age = $self->{AGE}; if ($age < 20) { eval "require Person::Baby"; import Person::Baby; } else { eval "require Person::Adult"; import Person::Adult; } return $self; } 1;

below is my test code

use Employee; my $qq = Employee->new( NAME => "rtrtr", AGE => 54, DEPT => "IT", ); $qq->sayHello(); $qq->sayHi(); my $qw = Employee->new( NAME => "rtrtr", AGE => 25, DEPT => "FINANCE", ); $qw->sayHi();

I'm getting an error "Can't locate object method "sayHi" via package "Employee::IT""