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

Hi there! I got a problem with perl cgi. If you have it generate a standalone checkbox, it surrounds it automatically with a label tag that keeps the caption of the checkbox. My problem is I need to change the attributes of this label. The official docs enclosed with the module tell nothing how this should be done. However the docs found on cpan.org tell something about a "-labelattributes" switch, but it's only mentioned within a bit of text but without any instructions of usage. Quickly going over the code of CGI.pm tells me that there must be a switch like the one mentioned, but I'm just not too good with perl to figure out how to use it right. I tried several possibilities how to make it work, but it didn't. The switch was never interpreted the right way. Can anybody tell me how to use this switch or how to work around this problem? The code I tried:
use CGI qw/:standard/; print checkbox(-name=>'my_checkbox', -checked=>0, -value=>'on', -label=>'label of my checkbox. Check Me!!!', -labelattributes=>[id=>'checkboxlabel_id']);
The html I got:
<label><input type="checkbox" name="my_checkbox" value="on" labelattri +butes="ARRAY(0x938ac80)" />label of my checkbox. Check Me!!!</label>

Replies are listed 'Best First'.
Re: perl cgi standalone checkbox label problem
by Your Mother (Archbishop) on Apr 06, 2009 at 20:21 UTC

    So close-

           -labelattributes => { id => 'checkboxlabel_id' }

    The instructions, CGI, with it say you need an associative array ref (hash ref), not an array ref. Just update as shown.

      Wow, that was a fast answer... Thanks! But unfortunately I got bad news for you. I already tried this too. Resulting code:
      <label><input type="checkbox" name="my_checkbox" value="on" labelattri +butes="HASH(0x9c9a888)" />label of my checkbox. Check Me!!!</label>
      So all that changed was that instead of Array(RefToArray), I got Hash(RefToHash) in my HTML output...

        Er... Then you didn't try it right.

        use CGI; print CGI::checkbox(-name => 'my_checkbox', -checked => 0, -value => 'on', -label => 'label of my checkbox. Check Me!!!', -labelattributes => { id => 'checkboxlabel_id' } ); __END__ <label id="checkboxlabel_id"><input type="checkbox" name="my_checkbox" + value="on" />label of my checkbox. Check Me!!!</label>