in reply to Re: Re: Re: Another Opinion Request...(config file)
in thread Another Opinion Request...(config file)

Ok, I think I understand now... Let me see if I can make it clear, and please tell me if I am way off base or not... :o)

Ok, I have these fields currently in my DB:
name content
I should add one called "type".

Then when I call the variables I should do it in one big swipe:
get_page_content("some_type");
Something like that, and it would then go load each value for that type?
Here is how I currently do it:
$default_font_family = get_page_content("default_font_family");
Each variables '$default_font_family' is the same name in the db: 'default_font_family'.
So how could I do that, where it will take the "name" and make it a variable, without telling it like I do: 'name = sub('name');'
Could I do something like this in the subroutine:
my $type = shift; my $dbh = DB_File::connect(); $sth = $dbh->prepare (qq{ SELECT name,content FROM page_vars WHERE + type = ? }); $sth->execute($type); while ( my ($db_name,$db_content) = $sth->fetchrow_hashref() ) { ${$db_name} = $db_content; } $sth->finish(); return 1;
Would that work? I don't think it would, but I have not tried it. Do you know off hand if it would work?

If not I suppose I could put them in a hash or array:
my $type = shift; my $dbh = DB_File::connect(); $sth = $dbh->prepare (qq{ SELECT name,content FROM page_vars WHERE + type = ? }); $sth->execute($type); my %vars; while (my ($db_name,$db_content) = $sth->fetchrow_hashref()) { $vars{$db_name} = $db_content; } $sth->finish(); return(%vars);
Then I could just have each variable instead of being called by the name be called by a hash:
instead of doing: $default_font_family = ... do this: %vars = get_page_content("some_type");
Then %vars would contain all the variables so I could print them by doing:
$vars{default_font_family}
The only bad thing about using the hash, is that I have already done a lot of work, where I don't do that, so I'd have to put a whole lot of time into search and replace :o)

Anyways, did I understand it the way you meant it?

Thank you much, as usual!
Richard

Replies are listed 'Best First'.
Re: Re: Another Opinion Request...(config file)
by draconis (Scribe) on May 13, 2003 at 15:13 UTC
    Looks to me like you got it !

    I would take the approach of using the hash to store the key/value pairs which end up being your variables with their respective values.

    Please let me know how you make out with this.