for my $k (keys %phrases) { # update # my $ref = $phrases{$_}; my $ref = $phrases{$k}; $ref->{ qtyTotal} = sub { qtyTotal($ref) } } sub qtyTotal{ my $qty = 0; my $phObj = shift; foreach my $qType ('qty', 'qtySum', 'qtyCont'){ $qty += $phObj->{$qType} if exists $phObj->{$qType}; } return $qty; }