Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Constant Variables

by dragonchild (Archbishop)
on Mar 27, 2003 at 23:31 UTC ( [id://246365]=note: print w/replies, xml ) Need Help??


in reply to Constant Variables

Modules that have use constant in them is a good start. Using your database to store such things is another good place, maybe in a table called constants with two columns - name and value (kinda like a hash). use Exporter is another good place. Also, using a Singleton object is a good place. There's a hundred different ways.

------
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.

Replies are listed 'Best First'.
Re: Re: Constant Variables
by powerhouse (Friar) on Mar 28, 2003 at 00:07 UTC
    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.

    thx,
    Richard

      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.

      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.
        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.

        thx,
        Richard
        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
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://246365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (3)
As of 2024-03-29 04:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found