http://qs321.pair.com?node_id=143555


in reply to Re: CGI.pm being funny?
in thread CGI.pm checkbox problem?

Actually,
print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>\@predefined, -defaults=>param('predefined'), );
does not appear to work. And instead fills the value and label of one checkbox with the arrayref. It returns something like:
<input type="checkbox" name="predefined" value="ARRAY(0x833ced0)" />AR +RAY(0x833ced0)<br />
Where as:
print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>@predefined, -defaults=>param('predefined'), );
Works perfectly well. And acts exactly the same as changing $, did. (except xhtml-ish br tag).
<input type="checkbox" name="predefined" value="element content" />ele +ment content<br />
The perldoc appears to say one thing and do another.
Even in its' own examples...
"...the second argument should be an array reference." and yet:
print $query->checkbox_group(-name=>'group_name', -values=>['eenie','meenie','min +ie','moe'], -default=>['eenie','moe'], -linebreak=>'true', -labels=>\%labels);
The perldoc also gives this example:
@h = $query->checkbox_group(-name=>'group_name',-values=>\@values); &use_in_creative_way(@h);
But this just fills @h with one element.
print $h[0]; --- <input type="checkbox" name="group_name" value="ARRAY(0x833d000)" />AR +RAY(0x833d000)
Very odd indeed.

Replies are listed 'Best First'.
Re: Re: Re: CGI.pm being funny?
by chipmunk (Parson) on Feb 06, 2002 at 05:37 UTC
    The reason for the behavior you are seeing is that @predefined contains an array reference, rather than a plain value. Since you don't show how you defined @predefined, it is impossible to say where you went wrong. Suffice it to say that:
    print $q->checkbox_group ( -name=>'predefined', -linebreak=>'true', -values=>@predefined, -defaults=>param('predefined'), );
    is not the right way to pass values to a CGI method. @predefined will be flattened into the argument list. If @predefined contains a single value, you've lucked out and it will work okay. If @predefined contains an odd number of values, the first will be taken as the value for the -values key, and the rest will be taken as separate key/value pairs. Worst case, if @predefined contains an even number of values, all the remaining explicit key/value pairs will be shifted by one, so that the keys are used as values and vice versa.
      I am splitting a "text" field in a database by newline.
      Each line is a different question.
      Each question is an element in @predefined. currently there are 30 elements.
      Everythings prints fine, and all values/labels match up.
      Just to test your odd vs. even theory, I added a question (new line to the text field).
      Everything still matches up. No shifting, no nastiness. Everythings works.
      Don't know what to say.

      update: Zaxo maybe, or maybe I am just crazy, or .. yeah, maybe. hrmph!
        Thanks, but I what I really want is to see the actual code where you define @predefined.

        Barring that, I would be interested to see what you get from this: print "@predefined\n"; My expectation is that you will see something like: ARRAY(0x100eb340) because @predefined contains a single element which is a reference to an array.