# Example of oop using Perl packages and "bless". Bless associates a # type tag with a structure. #------------------------------------------------------------- package RegularEmployee; sub new { my ($name, $age, $starting_position, $monthly_salary) = @_; my $r_employee = { "name" => $name, "age" => $age, "position" => $starting_position, "monthly_salary" => $monthly_salary, "months_worked" => 0, }; bless $r_employee, 'RegularEmployee'; # Tag object with pkg name return $r_employee; # Return object } sub promote { #... } sub compute_ytd_income{ my $r_emp = shift; # Assume the months_worked attribute got modified at some point return $r_emp->{'monthly_salary'} * $r_emp->{'months_worked'}; } #------------------------------------------------------------- package HourlyEmployee; sub new { my ($name, $age, $starting_position, $hourly_rate, $overtime_rate) = @_; my $r_employee = { "name" => $name, "age" => $age, "position" => $starting_position, "hourly_rate" => $hourly_rate, "overtime_rate" => $overtime_rate }; bless $r_employee, 'HourlyEmployee'; return $r_employee; } sub promote { #... } sub compute_ytd_income { my ($r_emp) = $_[0]; return $r_emp->{'hourly_rate'} * $r_emp->{'hours_worked'} + $r_emp->{'overtime_rate'} * $r_emp->{'overtime_hours_worked'}; } $emp1 = RegularEmployee::new('John Doe', 32, 'Software Engineer', 5000); $emp2 = HourlyEmployee::new('Jane Smith', 35,'Auditor', 65, 90); #Now use the arrow notation to directly invoke instance methods, or, as they say in OO-land, invoke methods on the object: # Direct invocation print $emp1->promote(), "\n"; print $emp2->compute_ytd_income(), "\n"; #mine print $emp1->{"name"}, "\n"; package account; sub newaccount { my $balance = $_[0]; }