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;