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

Fellow Monasterians,

In response to a question I asked earlier, I'm learning to use CGI to write some HTML for me (within the context of HTML::Template). However, when working with the OPTGROUP, using code copied right out of CGI's doc on CPAN, I'm getting an error:

Undefined subroutine &main::optgroup called at dropdowntests.pl line X
(3 in the code below).

Here is my Perl:

my $query = new CGI; my $opt = $query -> popup_menu( -name=>'menu_name', -values=>[qw/eenie meenie minie/, optgroup( -name=>'optgroup_name', -values => ['moe','catch'])], -labels=>{'eenie'=>'one', 'meenie'=>'two', 'minie'=>'three'}, -default=>'meenie'); my $template = HTML::Template->new( filename => '../dropdowntests.tmpl', die_on_bad_params => 1); $template->param( optiongroup => $opt); print "Content-type: text/html\n\n"; print $template->output();

I understand the what the Perl is seeing, but not sure why CGI is "allowing" it. Comments? Thanks.

Update: Solved. I started experimenting with different import functions and found that I could not just use the plain use CGI; and needed at least use CGI qw/:standard/;. To quote from the CGI doc:

To use the function-oriented interface, you must specify which CGI.pm routines or sets of routines to import into your script's namespace.


—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: CGI and optgroup menus has error
by Arunbear (Prior) on May 14, 2005 at 15:15 UTC
    CGI.pm does not export the optgroup() sub unless you ask for it e.g. by
    use CGI qw/:standard/;
    so you must invoke optgroup() as a method i.e.
    $query->optgroup(...)
    just as you have done with popup_menu().
      Even with both of those tweaks, the "Undefined subroutine CGI::optgroup" error still occurs ... Looking in CGI.pm, there is no optgroup() sub defined .. is it supposed to be available via AUTOLOAD?

      Update: It seems to work for OP now, but I still get his original error:
      use strict; use warnings; use CGI qw/:standard/; my $q = new CGI; $q->optgroup(); __END__ Undefined subroutine CGI::optgroup at /tmp/c line 6
      Update: Yup, it's cause i was working of an old (2.752) version.
        What version of CGI are you using? Versions before 2.86 (11 Sep 2002) don't have optgroup().

      Thanks Arunbear. I must have been typing my Update when you were replying. It was good to have confirmation of my findings.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot