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

I have a CGI script (in Perl) that shows a list of URL's based on previous inputs and computed values.
(i.e.
$urls .= 'weightwatchers.com' if $fat; $urls .= 'muscle.com' if $skinny;
)
Currently the list is hardcoded, in the middle of the script. I am looking for any elegant suggestions, on how I might have a list like this that would be a easier to add and update, yet still maintain the flexibility of all the ifs and if it could be simple enough that even (egad!) users could update the list. At this juncture I am leaning torward a hash in an external file that gets "used" in. Seems like there should be a better solution than that. This is a cross platform script that connects to an SQL database if that opens any thoughts.

I appreciate your thoughts.
g_White

Edited 2001-04-30 by mirod: changed title and added <code> tags

  • Comment on Seeking better solution to selecting from list of domains (was: Seeking a jewel....)
  • Download Code

Replies are listed 'Best First'.
Re: Seeking a jewel....
by little (Curate) on Apr 30, 2001 at 01:30 UTC
    # in you declaration at the beginnning of the script # or in an ini file %urlsForOccassions{ 'fat' => 'weightwatchers.com', 'skinny' => 'muscle.com' }; # in your script somewhere $urls .= $urlsForOccassions{'fat'} if $fat; $urls .= $urlsForOccassions{'skinny'} if $skinny;
    just as my 0.02

    Have a nice day
    All decision is left to your taste
    Update
    You can also then populate the hash from a CSV file or a DB to make it even more easy to maintain for nonprogrammers
Re: Seeking a jewel....
by DrZaius (Monk) on Apr 30, 2001 at 02:56 UTC
    The easiest way would be to create a table in your database that contains your entry. It is fairly easy to write a front end for updating this and to not worry about locking and such.

    Including a hash file is probably a bad idea, but if you want to do that, use a combination of Data::Dumper to write out the hash and Data::Undumper to load it back in. Data::Undumper comes with some cgi package and I recommend using it as it does the safe evaling for you.

    Also, if you were to use Apache::Pagekit, you could create the data in XML and create the code as component :)

    In order to reduce changes in your code ($urls .= 'weightwatchers.com' if $fat;), I recommend doing it this way:

    my $urls; # all the urls my %keywords = build_keyword_hash(%config_data); # get the list of keywords my @keywords = $cgi->param('keywords') foreach my $keyword (@keywords) { $urls .= $keywords{$keyword} if(exists($keyworlds{$keyword})); }
    cheers.
    (sorry about the overuse of 'keywords' for variable names)