http://www.perlmonks.org?node_id=636447

hacker has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to color a menu of items on an webpage, based on the item currently selected. I know the item chosen, based on $cgi->param('a'), so that part isn't a problem.

I've abstracted the logic into a small, standalone script that looks like this:

my @menu_options = ('home', 'donate', 'news', 'docs', 'download', 'gal +lery', 'samples', 'users', 'developers', 'about'); my $items = @menu_options; my $count = 1; print qq{ <div class="sh"> <span class="doNotDisplay">Navigation:</span> }; my $style = ''; foreach my $menu_item (@menu_options) { $style = qq{style="background-color: #9943ac;"} if ($action eq $menu_item && $action =~ /docs/); $style = qq{style="background-color: #d8402b;"} if ($action eq $menu_item && $action =~ /download/); $style = qq{style="background-color: #ab0;"} if ($action eq $menu_item && $action =~ /users/); $style = qq{style="background-color: #6fab87;"} if ($action eq $menu_item && $action =~ /developers/); print qq{\t\t<a href="/$menu_item" $style>$menu_item</a>}; print " |" if ($count < $items); print "\n"; $count++; } print "\t</div>\n";

In my actual code, I have a dispatch table that looks like this:

my %dispatch = ( (map { $_ => \&default } qw( home donate news docs download gallery docs users developers about )), samples => sub { \&samples($dbh) }, );

I'm trying to selectively color the background of the item selected. What am I missing in my sample code above?

I'm only trying to colorize 4 of the 10 menu options, and only one should be colorized at a time. They're in my dispatch table in the production code (not an array like the sample test above; I created that to try to isolate the problem).

Is there a better way to do this?