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

I have a CGI script that builds html forms, does DB access, DB creation, etc. I need to be able to easily modify it for others in our company to use. I thought I could use hash keys and values to accomplish this. I am integrating pager systems one uses a website the other files on a unix system. The web based script stores its information in a mysql DB. Different groups want to have their own system that stores different information in the DB and desire different displays of this information. Only a small portion of the DB data is required for the actual paging systems but folks would like to consolidate their information there. How to do this or is there a much better way. Here is my 1st idea a code sample that reads the form values specified in a hash, I would also use the hash(es) to build the DB, html forms, ect. -
use CGI; %var_name = ( opp => 'Operation', name => 'Name', ); for $vname (keys %var_name){ $statement='$'.qq!$vname = param('$vname')!; # this did not work eval{ $statement }; # nor did this ${statement}; print "$vname is $${vname}<br>\n"; }
This code would create these perl statements which I would need to have it execute.
$opp=param('opp'); $name=param('name');

Replies are listed 'Best First'.
Re: How to do dynamic stuff with perl
by Skeeve (Parson) on Oct 20, 2005 at 10:10 UTC

    Even after looking at your code I have absolutely no idea what you want to do. How about a bit of output data that you want to achieve?

    Update: Now I understand a bit more.

    You seem to want to populate global variables.

    Usually global variables are known when you write your program and creating some on the fly is most often not useful. You should rethink your approach as blazar stated...


    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e
Re: How to do dynamic stuff with perl
by blazar (Canon) on Oct 20, 2005 at 10:34 UTC
    In all earnestness, I can't understand what the question is. But whatever it is it seems that you want to build perl code as a string, and then to do something "useful" indeed you would have to eval it... Don't!! that is the answer! Use a dispatch table instead.
Re: How to do dynamic stuff with perl
by revdiablo (Prior) on Oct 20, 2005 at 15:05 UTC

    Your code doesn't work because it's using blockwise eval, instead of string eval. These are very different constructs.

    As the other reponses have mentioned, though, eval is probably not the best option. You should consider storing your values in a hash, then you don't have to mess with eval or soft references. Here's an example:

    use CGI; my %var_name = ( opp => 'Operation', name => 'Name', ); my %values; for my $vname (keys %var_name){ $values{$vname} = param($vname); print "$vname is $values{$vname}<br>\n"; }
      Thanks that could work quite well for me. It is what I wanted to do originaly but I was using a string where a hash is more suitable for what I wanted to do but I did not see that.