in reply to is there anything more VEXING than checking the $cgi->checkbox ?

In CGI.pm 4.60, the code you showed works as advertised. Here's an SSCCE:
use warnings; use strict; use CGI; my $cgi = CGI->new(); print "CGI.pm Version: $CGI::VERSION\n"; print $cgi->checkbox( { -name => 'cb-1', -value => 1, -label => "", -c +hecked => 'checked' } ); print "\n\n"; __END__ CGI.pm Version: 4.60 <label><input type="checkbox" name="cb-1" value="1" checked="checked" +/></label>

So I don't know what you're doing wrong, but it should work.

Aside: I don't know whether you've read the documentation in the last few years, but CGI.pm makes it pretty clear that its HTML Generation functions should no longer be used, and explains the reasons. (That note has been there since 4.04 released in September 2014.) As it explains, though it's recommended to use a full framework when possible, it is still reasonable to use CGI.pm for handling the input parameters and otherwise handling the Common Gateway Interface requests and responses (though CGI::Fast or others give the same hooks in a smaller package), but you should definitely use a templating toolkit of some sort for actually generating your HTML.

  • Comment on Re: is there anything more VEXING than checking the $cgi->checkbox ?
  • Download Code

Replies are listed 'Best First'.
Re^2: is there anything more VEXING than checking the $cgi->checkbox ?
by misterperl (Friar) on Nov 20, 2023 at 14:38 UTC
    We're on CGI 4.51 perhaps an update is required. This is an odd presumption: I don't know what you're doing wrong. Perhaps it's more accurate to suggest I don't know what the CGI checkbox sub author was smoking that day!

    CGI.pm sub checkbox states "$checked -> (optional) turned on by default if true

    Suggested that any value other than 0, '', or undef, should "turn on" checked. What "turned on" means is unspecified. It should result in the word "checked" in the HTML . Im passing "on" and it's not there.

    I edited the perl lib module CGI.pm .

    First I added die $checked; and it said "on". So far so good.

    Then I removed that, and at the end where it "returned", I changed that to die. And I get:

    <label><input type="checkbox" name="dev" value="on" class="readonly" / +></label>

    proving my checked is set to "on",and its missing in the result.