http://qs1969.pair.com?node_id=246400


in reply to Re: Re: Re: Constant Variables
in thread Constant Variables

Actually, what I want to do, is have all my variables in the column 'name'. They are not yet, PERL strings. I want to make them Perl strings, containing the value in the column 'valu', of the same record id.

For instance, I have these in there so far..

name valu
default_font_family Arial, Helvetica, sans-serif
default_font_color #000000
standout_text_color #F52D37
disabled_text_color #C0C0C0

Those are the top few. I want the values in the column 'name' to be Perl Strings, that contain the VALUE in column value.

Like these examples that do NOT work:
$dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my $settings = $sth->fetchrow_hashref()) { use CONSTANT $settings->{name} => $settings->{value}; } $sth->finish(); # OR $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my $settings = $sth->fetchrow_hashref()) { $$settings->{name} = $settings->{value}; # OR ${$settings->{name}} OR @{$settings->{name}} I've tried a +ll 3, nothing is working. } $sth->finish();
So now, when I get to a "form" or something, if I know I have the variables in column "name" above, I can put the strings. such as this...

print qq~Welcome to <font style="font-family: $default_font_family; co +lor: $default_font_color">Company Name</font>~;
Something like that. The reason they are in a database, instead of just in a file, is that I'm making an admin interface, where they can be editted, easily, by my partners, who do not know perl at all, so they can make changes without having to get into the source code.

If I still did not make it clear what I'm trying to do, please let me know, I'll try to clarify it again ;)

thx,
Richard

Replies are listed 'Best First'.
Re: Re: Constant Variables
by The Mad Hatter (Priest) on Mar 28, 2003 at 16:23 UTC
    I think this might work (though I'm at school and I can't test it):
    $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); my %settings = %{$sth->fetchrow_hashref}; # I'm assuming you only + have one row... foreach my $key (keys %settings) { my $varname = "$" . $key; eval "$varname = $settings{$key}" or die "problem with eval"; } $sth->finish();
    Also, from what you've said, it seems as if you are just using the settings to globally control the properties of HTML text so you only have to change the property in one place. If this is the case, then you should use CSS (Cascading Style Sheets) to define site-wide styles and include the CSS file in every page. CSS is a very powerful mechanism for easily changing the layout and design of a site.

    Update: Hmm, I just saw that you want to use the values in the column name as the variable name. The code above uses the name of the field (or column) in MySQL as the variable name. Here is the code to do what you want:

    $dbh = CWT::Site_DB::connect(); $sth = $dbh->prepare (qq{ SELECT * FROM config_settings }); $sth->execute(); while (my %settings = %{$sth->fetchrow_hashref};) { my $varname = "$" . $settings{name}; eval "$varname = '$settings{value}'" or die "problem with eval"; } $sth->finish();
    That should do the trick, but it is also untested.