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

hi,

i'm new to perl and i need some serious help to eliminate some long coding. here is a snippet that i use along with a subroutine to capture form input:

$a = $in{'a'}; $b = $in{'b'}; $c = $in{'c'}; $d = $in{'d'}; . . .
you get the point. and it becomes very long. here is my idea:
$fieldList = $in{'fieldList'}; @fieldList = split(",",$fieldList); foreach $field(@fieldList){ $$field = $in{'$field'}; }

$filedList holds a list of all the fields it a form (a,b,c,...), but it doesn't work. i'm i being greedy? stupid? or just a newbie? HELP!

Edit: chipmunk 2001-05-30

Replies are listed 'Best First'.
Re: variables for variables
by myocom (Deacon) on May 31, 2001 at 00:25 UTC
(boo) Re: variables for variables
by boo_radley (Parson) on May 31, 2001 at 00:46 UTC
    UPDATE

    I misunderstood your request. What you're asking for can be a giant pain to troubleshoot. Therefore, my brain blocked out your real question, and replaced it with something very close syntactically.
    Now, I don't know if you know dominus, but he's usually a pretty mellow guy. Very helpful, always cheerful, & etc.. However, allow me to quote from the article myocom showed you :

    It turned out that the clods who had written this program had done something like this:
    while (<RECORDS>) { chomp; @values = split /\t/, $_; foreach $v (@values) { $$v++; } }
    you'll see that this bears a startling resemblance to what you've thought of. Don't do it.

    Instead, he suggests

    If the original programmers had used a series of hashes instead of stuffing everything into a bunch of global variables, it would never have happened, or at worst it would have been easy to fix. I ended up doing a major overhaul on the program to solve the problem.
    Which is what I thought you needed help on. OK? OK.

    now, back to the original post


    here's one way :

    for (a..z) {$in {$_}=int rand(100) }; foreach (keys %in){ print "in $_ is $in{$_}\n"; $$out{$_}=$in{$_} } foreach (keys %$out){print "out $_ is $$out{$_}\n"}
    the magic in line 4 works like this :
    $out is normally a simple scalar. however, prepending it with another "$" and tacking on the {$_} makes $out a reference to a hash.
    That's why we can use %$out in line 6. Using the "%" dereferences $out.
    Similarly if we say  $foo=\$bar, $foo is holding a reference to that scalar, it's not a scalar in itself. You can dereference it with $$foo. Also,  $foo=\$bar can be written like  $$foo=$bar, which means "the scalar referenced by $foo set to $bar's value". Make sense?

    When you do

    foreach $field(@fieldList){ $$field = $in{'$field'}; }
    , what you're doing is running through each element in @fieldlist as $field, and overwriting it with a reference to a scalar that's set to $in{'$field'}. Additionally, this doesn't make any sense either, since single quotes never do interpolation. Use double quotes or just get rid of them altogether, like $in{$field}.
    Also, use strict and -w.
    Finally, what reasons do you have for this reassignation of values? Will you really be transforming each $in variable? Or are you just wanting a temporary place to work with them?

    If it's the later, dispense with this idea altogether, and just use %in in place.

    Update : also, see tye's nifty-keen explanation of referencing and dereferencing for more details.

Re: variables for variables
by Masem (Monsignor) on May 31, 2001 at 00:30 UTC
    While I know there's a way to do this, why are you doing this in the first place? Sure, it's easier to type out "$a" than "$form{'a'}", however, not only do you increase your memory requirements, you can also easily introduce errors because of it.

    Mind you, there are some good reasons to do this, but this usually isn't the case 99% of the time.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
(zdog) Re: variables for variables
by zdog (Priest) on May 31, 2001 at 00:25 UTC
    split() has to used in the following way:

    split (/\,/, $fieldList);

    Also, it would be helpful for you if you took the extra step and inserted use strict; and declared variables using my().

    Zenon Zabinski | zdog | zdog7@hotmail.com

      You're wrong about split. It can take a string and interpret it as a regex. You don't need to escape commas in a regex, either. If you want to, fine, but it's not necessary.

      Using strict is just going to make the code fail worse. Not that that's a bad thing.

      The problem is the single quotes in $in{'$field'}. But please, please read Dominus's article (mentioned above) about why you shouldn't be doing this in the first place.

Re: variables for variables
by novitiate (Scribe) on May 31, 2001 at 02:39 UTC
    If you must, then PHP {yes, it is more than an embeddable HTML lang}.
    But please heed the advice of your brethren.

    humbly,
    novitiate