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;
####
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();