package Qty; sub new { my $class = shift; bless { @_ }, $class; } sub qtyTotal { my $phObj = shift; foreach my $qType ('qty', 'qtySum', 'qtyCont'){ $qty += $phObj->{$qType} if exists $phObj->{$qType}; } return $qty; } package main; my %phrases = ( "phrase 1" => Qty->new( qty => 1, qtySum => 5, qtyCont => 8, ), "phrase 2" => Qty->new( qty =>10, qtyCont =>34, ), "phrase 3" => Qty->new( qty =>1, ), ); print "${ \$phrases{'phrase 2'}->qtyTotal() }\n";