in reply to Re: Populating an Array of hash's
in thread Populating an Array of hash's

Thanks, that actually did the trick, but I have a question. What does the line below do? It looks like its taking the @files array and mapping them into a hash, which is getting mapped to the array FILES which HTML::Template then parses??
$template->param( FILES => [ map +{ NAME => $_ }, @files ], );
What is this line doing?
map +{ NAME -> $_}

Replies are listed 'Best First'.
Re^3: Populating an Array of hash's
by ikegami (Patriarch) on Oct 02, 2008 at 02:13 UTC

    map +{ NAME => $_ }, LIST
    is another way of writing
    map { { NAME => $_ } } LIST

    The expression { NAME => $_ }

    1. creates a hash,
    2. assigns NAME => $_ to it (creating a key named NAME whose associated value is $_), and
    3. returns a reference to the hash.

    The expression map { { NAME => $_ } } @files

    1. For every element of @files,
      1. sets $_ to the element
      2. executes the sub-expression.
    2. A list is formed from the result of the sub-expressions and returned.

    In other words,

    $template->param( FILES => [ map +{ NAME => $_ }, @files ], );

    could have been written

    my @files_param; for (@files) { push @files_param, { NAME => $_ }; } $template->param(\@files_param);