in reply to HTML as radio_group label w/CGI.pm

Your code could definitely benefit from use strict; and use warnings; — you're declaring a hash (and also creating it at the same time), and then you try and treat it like an array when CGI.pm actually expects a hash reference (you can do magic to make something like this work, but you don't want to).

Something like this does what you mean:

use warnings; use strict; use CGI qw(:standard); use Data::Dumper; my %labels = ( monkey => "sigmund", cat => "felix", dog => "fido", ); print radio_group(-name=>'clearchannel', -values=>[keys %labels], -labels=>\%labels);
Here I'm creating a hash (the my %labels lines). Pass the list of possible values for the group (monkey; cat; dog) as an array reference created by the [] operator (keys merely returns the keys of a hash as a list). Then provide the labels for each of the values by passing a hashref.


davis
Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.

Replies are listed 'Best First'.
Re^2: HTML as radio_group label w/CGI.pm
by Anonymous Monk on Sep 25, 2005 at 15:54 UTC
    Actually, I am using strict in my code (though not warnings) -- I just used a small slice of a larger program, since I didn't want to inundate everyone with a bunch of superfluous code.

    While what you've suggested above is very handy -- it does a nice job of cleaning up both my code and memory -- it totally misses the point of what I was asking. The heart of my problem is that, when I have HTML tags in a radio button label, they're spit out inside of <label> tags such that web browsers see them as literal text, not actual HTML, and they don't get properly rendered. For example, instead of getting the string "Year:" followed by a select box on a new line, I'm getting:
    Year:<br><select name="year_year" tabindex="1"> <option selected="sele +cted" value="2005">2005</option> <option value="2006">2006</option> < +option value="2007">2007</option> <option value="2008">2008</option> +<option value="2009">2009</option> <option value="2010">2010</option> + </select>
    Which is distinctly not helpful. Looking at the page source, I'm seeing:
    <label><input type="radio" name="date_selection" value="year" tabindex +="7" />Year:&lt;br&gt;&lt;select name=&quot;year_year&quot; tabindex= +&quot;1&quot;&gt;&#10;&lt;option selected=&quot;selected&quot; value= +&quot;2005&quot;&gt;2005&lt;/option&gt;&#10;&lt;option value=&quot;20 +06&quot;&gt;2006&lt;/option&gt;&#10;&lt;option value=&quot;2007&quot; +&gt;2007&lt;/option&gt;&#10;&lt;option value=&quot;2008&quot;&gt;2008 +&lt;/option&gt;&#10;&lt;option value=&quot;2009&quot;&gt;2009&lt;/opt +ion&gt;&#10;&lt;option value=&quot;2010&quot;&gt;2010&lt;/option&gt;& +#10;&lt;/select&gt;</label>
    That said, does anyone know how to make the HTML in my label actually render as such, and not output directly to the screen?

    Thanks,
    Alex Kirk