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


In reply to Can't locate object method- Issue by perl_noobie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.