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

i am building a drop down with options 1..31 using a subroutine. Also im using GCI::Pretty module. i am getting a compilation error in the following code. Please help me out.
&build_combo('d'); # function call sub build_combo{ my ($type)=@_; my ($day,$month); my ($i); if ($type=='d'){ $month = $cgi->Select({-name=>"days"}, [ for ($i==0; $i<=31; $i++){ $cgi->option({-value=>"$i"},"$i"), } ] ); }#endif return $month; }#endsub

Replies are listed 'Best First'.
Re: building combo box
by davis (Vicar) on Sep 17, 2006 at 10:03 UTC
    The bit that's failing is the "for" inside the array ref — that just can't work. Besides, it looks like you might be trying to:
    #!/usr/bin/perl use warnings; use strict; use CGI; my $cgi = CGI->new(); print $cgi->popup_menu(-name=>"Days", -values=>[ 1 .. 31]);
    Although this isn't the correct way of doing it — you should use one of the DateTime modules to get the number of days in the month (How many days are there in February 2000? February 2004? February 2100?)

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

      A for inside the array ref won't work, but for future reference a map would work fine (but yes it's unneeded in this case since it's easily made with ..-as-list-constructor).

      my $array = [ map { transmogrify_foo( $_ ) } 1..31 ];
      davis has given you an appropriate answer, I would like to add that you should refer to the LATEST documenation for a module. The Select method has been deprecated for a long time, as far back as 1998 and at a quick check it doesn't appear in the current CGI documentation. I don't know where you found the information you have based your code on, but it is more than a little out of date.

      You might like to refer to Ovids CGI course, it will guide you along a well founded path.

      jdtoronto

        The Select method has been deprecated for a long time, as far back as 1998 and at a quick check it doesn't appear in the current CGI documentation

        It may be deprecated, but it's there. Look in Non-standard HTML Shortcuts.

      Thank you your code is working fine for me
Re: building combo box
by friedo (Prior) on Sep 17, 2006 at 17:49 UTC
    In addition to what the others said, you don't want to use == for comparing strings. == is for numerical comparison; eq is for strings. If you had warnings on, you would see "Argument "d" isn't numeric in numeric eq (==)..." in your error logs.