This is related to Problems with 'strict' and variables created on the fly but a different application. I have a number of items I want to get from an HTML form and would like to put these into simple variables. I could put the data into a Hash but I'm using the data in SQL statements which result in very long, difficult to read, lines.

Here's what I've done so far. Examples 1 and 2 do not work. Examples 3 and 4 work correctly.

use strict; use CGI; my $q = new CGI; # 1. The simple solution:... which will NOT work with 'strict'. Cannot + create variables on the fly ($$fieldname) @fields = $q->param; foreach $fieldname (@fields) { $$fieldname = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld } # 2. I applied 'strict' and I got the following errors. # Global symbol "varA" requires explicit package name... # Global symbol "varB" requires explicit package name... # etc for each variable.</i> # # So I tried this. The code ran without error, but nothing got assigne +d to the variables. my ($varC,$varB,$varA,$varD,.....); # an unsorted list of the expected + field names my @fields = $q->param; foreach my $fieldname (@fields) { no strict; $$fieldname = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld } # 3. A workable solution but, if the incomming data gets corrupted, it + could end up in the wrong variables. my @tempdata; my $i = 0; my @field = sort $q->param; # sorted list of the field names foreach my $fieldname (@fields) { $tempdata[$i] = $q->param($fieldname); # within this loop: also check for specific, or no data on each fi +eld $i++; } my ($varA,$varB,$varC,$varD,.....) = @tempdata; # $vars in sorted orde +r # 4. Using a hash which works as expected. my %data; my @field = $q->param; foreach my $fieldname (@fields) { $data{$fieldname} = $q->param($name); # within this loop: also check for specific, or no data on each fi +eld }
Is there a simple way to accomplish this? or Should I use a Hash; and be done with it? or Simply not use strict? What's the downside in a relatively short program (350 lines)?

In reply to Creating variables while using 'strict' by nedals

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.