in reply to Re: Constant Variables
in thread Constant Variables

I like this method...
maybe in a table called constants with two columns - name and value (kinda like a hash).

That is what I'm doing, but how do I make it where the column "name" is like a variable, for instance if record 1 was this:
color1 red

How could I get it to have $color1 contain the value?

In other words, in case I did not make it clear...

$string1 = qq!Hello, my favorite color is $color1!;
How would I get using a database to do that, using Perl?
I don't see how the Export would do that, or the other things you showed.


Replies are listed 'Best First'.
Re: Re: Re: Constant Variables
by The Mad Hatter (Priest) on Mar 28, 2003 at 00:32 UTC
    It sounds as if you want to bind the variable $color1 to the value of a the corresponding column in the current row. Check out the bind_col and bind_columns methods of DBI.

    Update: As CPAN appears to be down, here is the basic syntax (grabbed from perldoc DBI);

    $rc = $sth->bind_col($column_number, \$var_to_bind); and $rc = $sth->bind_columns(@list_of_refs_to_vars_to_bind);
    The variables pretty much explain themselves ;-), but if you don't get it, do a perldoc DBI (or equivalent) and find the complete doc.
      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 ;)

        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.
      Would it be easier to just us a "flat file" type of database?

      I think it might be easier to just use a seperate file called something like settings.config and then in the admin interface, make it where when the edit the settings, it just OPENS it, and gets the variables that way, then it can re-write the file everytime the administrator updates the values.

      I know how to do that. I just thought it might be faster to just put them in my MySQL database. Of course, If I'm only requiring a config file, then it would probably be faster then running through a database and trying to assign variables.

      At least in the config file I could make it look like this:
      $default_font_family = "whatever"; $default_font_color = "whatever2";
      Do you think that would be easier? I'm starting to feel that way, since there does not seem to be an easy way to do it with a Database. At least from what I seen thus far.

        Yes, since it is just config, a flat file would probably be easier to do, although if you really wanted, I think it is possible to do the MySQL thing (looping through and binding each column is the way to go, I think).

        If you defined the variables in the config file like you said, all you would have to do is a do filename; and you'd have the variables.

Re: Re: Re: Constant Variables
by zakb (Pilgrim) on Mar 28, 2003 at 11:17 UTC

    I can't see how you can do well what you want; others more experienced may well have a trick to do it. Maybe something along the lines of:

    my $x = "variable_name"; my $value = "value"; eval "\$$x = $value"; warn $@ if $@; print $color1;

    Which works, but not under use strict.

    My suggestion would be to change your approach to use a hash. This would greatly simplify things.

    my %config; # use database (DBI) to read key / values into hash # ... $config{color1} = "red"; print $config{color1};

    Update: Just re-read your question. You want a way to provide access to the %config hash on each page. You could use a module to do this, exporting the %config hash.

Re3: Constant Variables
by dragonchild (Archbishop) on Mar 28, 2003 at 14:43 UTC
    You don't want to do that. You want to have a hash of configuration values. That way, you do something like:
    my $string1 = qq!Hello, my favorite color is $config{color1}!;

    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.