foreach $products (@{$file->{product}}) {
print "Widget: " . $products->{title} ." \n";
print "Rating: " . $products->{rating} . "\n";
print " Cost: " . $products->{cost} . "\n";
print "Colors: " . join(', ', @{$products->{color}})."\n\n";
}
####
widget3
0.50
S
white
####
widget1
0.10
B
red
blue
green
widget2
0.25
S
pink
gray
orange
####
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";
}