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

I've got a program where I have something like this:
$hashref = { key => ['value1', 'value2'], key2 => ['value3', 'value4'] + };

This is a nice little conversion trick where the keys are known CGI parameters, the first value in the array refers to a specific database column, and the second is for spitting out something nice if there's an error. This allows me to use one piece of code to do similar tasks in multiple tables in the database.

Now, I'm running into a new problem. I now have some CGI parameters that will not be known until runtime. I want the frontend script to admit these new parameters into the above reference based on what I do know about the parameters before runtime. I think this should work, but my eyes are playing tricks on me and making my brain stare in disbelief. Here's the admittance code:

foreach $param ($cgi->param) { if ($param =~ /^abc_/) { $hashref->{$param} = ['column', 'text']; } }

Why do I have the feeling of disbelief? Am I just crazy? Thanks for anyone who can see whether I've correctly addressed all the levels of anonymity here.

PsychoSpunk

P.S. I would just run this but I'm in the middle of a very big script that this is located in, and if it broke later, I would just post this message but with the statement that goes, "I can't make this work." This message is simply proactive debugging. :) ALL HAIL BRAK!!!

Replies are listed 'Best First'.
Re: Referents Anonymous.
by chromatic (Archbishop) on Sep 30, 2000 at 00:23 UTC
    Looks like it should work to me. You could test it with something like the following:
    my $hashref = {}; my @params = qw( abc_1 abc_2 abc_3 cba cdr car ); foreach my $param (@params) { if ($param =~ /^abc_/) { $hashref->{$param} = [ 'column', 'text' ]; } } use Data::Dumper; print Dumper($hashref);
    Assuming you trust your ability to get reliable data from CGI::param() (and there's no reason not to), you should be safe.

    I can't see any reason why this wouldn't work right. However, I'd probably skip the if check and build a hash of allowed parameters and array references and do something like:

    foreach my $param ($cgi->param()) { if (defined(my $arr_ref = $options{$param})) { $hashref->{$param} = $arr_ref; } }
    It depends on what you're doing with $hashref, I suppose.
      Thanks chromatic, I just needed a sanity check. It's been one of those days. Plus, this has been one of the first times that I've had a specific piece of code that I felt would be of use to others, and would also give me a chance to see MTOWTDI. I'm not tied to the current act of building the hashref, but the hashref is now in place so that I can't make significant changes to the end result. But in my case, the parameters that I put in at runtime do have known values in the anonymous array. It happens that I just don't know the keys.

      ALL HAIL BRAK!!!