Title: foreach and an array of hashes

is it possible to foreach through and array of hashes
and reference the objects attributes?
In testing I am just trying to print each employee name.

my @emp;

$emp[0]=new_employee("Bob",50,"Cook");
$emp[1]=new_employee("Jane",20,"Receptionist");
$emp[2]=new_employee("Joe",25,"Cashier");

foreach my $curemp (@emp) {
   print $curemp{name};
}

sub new_employee {
    my ($name, $age, $starting_position) = @_;
    my $r_employee = {                  # Create a unique object
        "name"     => $name,            # using an anonymous hash
        "age"      => $age,
        "position" => $starting_position
    };
    return $r_employee;                 # Return "object"
}

TIA

Reply via email to