The map() function
function is like a rubber stamp applied to all the elements of a list
(see "perldoc -f map" for more information). It consists of two parts:
a block or an expression, and a list:
foreach() loop will usually do better than
map() in benchmarks, so don't use
for CPU-intensive calculations unless you test its performance. Do use
it when it makes your code more elegant and simple without a loss of
There are many neat things that
map() can do. First of all, it can modify the array elements as it goes through them by changing the
$_ variable. Inside the block or (less commonly) the expression,
$_ is the current element of the list. You don't know which element you
are looking at -- the whole point is to map one single function to all
the elements independently. It is my experience that in about 80% of
loops over an array you don't need to know the offset of the current
element inside the array. Thus,
map() can improve coding efficiency and style by forcing the programmer to think independently of array offsets.
In Listing 2, you can see how
the FP way is not fundamentally different, yet manages to convey a flow
of functions from right to left. First the list of keys is obtained,
then it is sorted, then the
print() function is applied to each element of the sorted key list.
Listing 3 demonstrates how the list passed to
map() can be completely rewritten. In this case, the array @users contained only user names, but after
was applied, the array contained hash references with one key-value
pair, username => byte length of user name. How about quickly
filling in file information?
In Listing 4 we create a list of files, and then in one statement create a list of hashes with entries for the name, size, and mtime (modification time) of each file. Furthermore, non-existent files get an "undef" reference instead of a hash reference. Finally, Data::Dumper is used to print out a nice view of the entire product list.
Needless to say, code like Listing 4 should be heavily documented for other people's sake. Don't try to cram it all into one line, either. Elegant code is just an ugly duckling without proper formatting and comments.