use strict; use warnings; my %v; $v{hourly_worker_expenses} = { # elaborate on this ! 'all' => 16 }; $v{hourly_worker_cost} = &{sub { my $cost = 0; my $item; my %costs = $v{hourly_worker_expenses}; #print %costs; foreach $item ($costs) { $cost += $costs{$item}; } #print $cost; return $cost; } }(); print $v{hourly_worker_cost};