sub list { if (defined(ref($_[0])) && ref($_[0]) eq 'ARRAY') { return @{ $_[0] }; } else { return $_[0]; } } foreach $products (list($file->{product})) { print "Widget: " . $products->{title} ." \n"; print "Rating: " . $products->{rating} . "\n"; print " Cost: " . $products->{cost} . "\n"; print "Colors: " . join(', ', list($products->{color}))."\n\n"; }