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

Hi all,

Let's say you had to print a whole bunch of radio buttons while inside a loop, and that you then wanted to call them using the param function.

The param function would look like this:

if (! param ) { show_form(); } elsif (defined param ('file_chosen)) { parse_text(); } elsif (defined param('$temp_name')) {print print_results(); }

and somewhere down the file, you have something like:

while ($counter<=$#doc_offset_array) { @temp_array = @{$array_of_arrays[$counter] }; $temp_name =$doc_offset_array[$counter]; foreach $i (@temp_array) { print radio_group(-name=>'$temp_name', -values=>[@temp_array], -linebreak=>'true'); } } }

My problem is that I can't get param to handle a scalar as an argument.

If I use single quotes in both sets of code, the elsif statement executes, but the radio buttons are all in one group rather than separate groups.

If I just use $temp_name (without quotes) in the radio_group, and single quotes around it in the param function, the radio buttons are fine, but the elsif statement doesn't execute.

Any suggestions?

Thanks

2001-03-28 Edit by Corion : Added CODE tags and tried to fix the indentation

Replies are listed 'Best First'.
Re: Param and Scalars
by Masem (Monsignor) on Mar 28, 2001 at 07:36 UTC
    First, when you post code, make sure you encase it in PM's special <CODE> & </CODE> tags, which will format everything nicely and remove problems with special HTML characters.

    As to your specific problem, remember that strings with single quotes do NOT get interpolated, that is, your $temp_name variable will not be expanded, and perl will treat the string as "$temp_name". Either you want to use no quotes, which will include the value of the variable directly, or use double quotes, which will expand any variables it can in addition to other text that you have. This is probable why most of your code isn't working.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Sorry about that. Here are the two snippets of code with the appropriate tags around them?

      The param stuff:

      if (! param ) { show_form(); } elsif (defined param ('file_chosen)) { parse_text(); } elsif (defined param('$temp_name')) {print print_results(); }
      The radio button group:
      while ($counter<=$#doc_offset_array) { @temp_array = @{$array_of_arrays[$counter]}; $temp_name =$doc_offset_array[$counter]; foreach $i (@temp_array) { print radio_group(-name=>'$temp_name', -values=>[@temp_array], -linebreak=>'true'); } } }

      Actually, using no quotes in both the radio group and the param function still prevents the elsif statement from being executed, though the radio buttons print OK. Using double quotes in both sets of code also prints radio buttons OK, but prevents the elsif statement from being executed. L

        It looks like you haven't cut-and-pasted - for example, you should have

        .. elsif (defined param ('file_chosen'))

        .. but obviously, you don't, otherwise it probably wouldn't even parse. The other one ought to be something like

         elsif (defined param ("$temp_name"))

        (personally, I prefer double quotes, most don't.)

        I think you ought to try and post the actual code, rather than a pseudocode example, as there are lots of other things wrong with this which look more like someone trying to express what they want to do rather than the code they already have.