# usage: special_last_map { block } $sentinel_variable, @list_of_values; # You should examine the value of the sentinel variable inside your code block. # It will be True for the last item in the list; False otherwise. sub special_last_map(&\$@) { my $code = shift; my $is_last_sr = shift; my $n = $#_; map { $$is_last_sr = $_ == $n; local $_ = $_[$_]; &$code } 0 .. $#_ } #### my $is_last; my @list_in_html = special_last_map { my $attr = $is_last ? ' class="last"' : ''; "$_" } $is_last, @list; #### sub map_with_index(&@) { my $code = shift; my $n = $#_; map { local $a = $_; local $b = $n - $_; local $_ = $_[$_]; &$code } 0 .. $#_ } #### print for map_with_index { my $class = ( $a == 0 ? 'first' : '' ) . ( $b == 0 ? 'last' : '' ); my $attr = $class ? qq( class="$class") : ''; "$_\n" } qw( alpha beta gamma delta );