foreach $result (@results) {
foreach $field (keys %$result){
print "$result->{$field}";
print "<br>";
}
} could be written as
map
{$result = $_; print map
{ "$result->{$_}<br>" } keys %$_} @results;
or better
map
{$result = $_; print join '<br>', map
{ $result->{$_} } keys %$_} @results;
Andreas
map runs through a list, performs something on each element, and returns a new list of the resulting values.
Inside the code part of map, $_ represents the active element from the input list. It is a reference, so the list can be changed, also inadvertently, by changing $_.
This code takes an array of strings and returns an array of the lengths:
@lengths = map { length } @strings;
This adds a final . to all elements missing it:
map { /\.$/ or $_ .= '.' } @array;
I use it all the time because it makes very compact code. It is quite readable for experienced perl programmers, probably less so for the less experienced programmers and for those without knowledge of functional programming.
map is described in the perlfunc manual page.
René.