I tried to make it as neat as possible.
Go for here-documents (perldoc perldata) to reduce the clutter of the single quotes and concatentations and double-quoted newlines:
use strict; use warnings; my %categories = ( A => [ 'B', 'Z' ], C => ['D', 'E'] ); my $category = param('category') || ''; my $subcategory = param('subcategory') || ''; use CGI qw/:standard/;; # Generate javascript print <<EOF; <script language="javascript"> function catChange(newCat) { with(document.your_form.subcategory) { options.length=0; switch(newCat) { EOF foreach my $c (keys %categories) { print <<EOF; case "$c": EOF my $i = 0; foreach my $s (@{$categories{$c}}) { my $opts_params = ($s eq $subcategory && $c eq $category) ? ',true,true' : ''; print <<EOF; options[$i]=new Option("$s" $opts_params ); EOF ++$i; } print <<EOF; break; EOF } print <<EOF; case "Category": options[0]=new Option("Please choose a category"); break; } } } </script> EOF # Well, now wasn't that fun?! # Beginning of form print start_form(-name=>'your_form'); print p(popup_menu(-name=>'category', -values=>['Category',sort keys %categories], -onChange=> 'catChange(this.options[this.selectedIndex].val +ue);'), popup_menu(-name=>'subcategory', -values=>['Please choose a category']));
Also, i did the following: Another good way to tidy up the print statements besides here-documents is printf -- for example:
printf 'options[%d]=new Option("%s" %s );\n', $i, $s, $opts_params;
And of course there is always Template Toolkit or HTML::Template and others, but that's probably overkill in this case.

In reply to Re: Double popup_menu by davidrw
in thread Double popup_menu by wink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.