solitaryrpr has asked for the wisdom of the Perl Monks concerning the following question:
I have some code that I'm working on that needs to be able to set system variables similar to 'source /my/config.cfg' in unix. I've found a really neat way to convert these to perl variables dynamically with the following code:
#!/usr/bin/perl # Set up the environment variables and dynamic variables from /my/conf +ig.cfg foreach (`. /my/config.cfg; set`) { chomp; my ($var,$val) = /^\s*(.*?)\s*=\s*(.*)/; ${$var} = $val; }
if my config file had the following:
myvar1 = foo myvar2 = bar
I should get the equivalent perl variables '$myvar1="foo"; $myvar2 = "bar";'...
This works well until I add 'use strict;' which is what you'd expect...I have to add 'no strict "vars"; no strict "refs";' in order to have the code work...
I've looked on the web and around the monks site but I haven't hit on a solution that will allow me to use strict without crippling it. I've read several perl faqs for pulling embedded vars out of text files using eval but nothing I've come across covers this situation exactly.
Looking for a pearl of wisdom that will make this all better (I am currently crippling strict).
-solUpdate...thanks for all the input...a hash is probably best for my needs...I'm thinking of using the $ENV{$var} hash to make them environment variables...probably should have done that to start with but it was 3 AM and I wasn't thinking too clearly.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dynamic variables and strict
by dewey (Pilgrim) on May 02, 2007 at 20:44 UTC | |
by FunkyMonk (Bishop) on May 02, 2007 at 22:35 UTC | |
|
Re: Dynamic variables and strict
by kyle (Abbot) on May 02, 2007 at 20:44 UTC | |
|
Re: Dynamic variables and strict
by varian (Chaplain) on May 03, 2007 at 06:24 UTC | |
|
Re: Dynamic variables and strict
by McDarren (Abbot) on May 03, 2007 at 04:59 UTC | |
|
Re: Dynamic variables and strict
by naikonta (Curate) on May 03, 2007 at 15:40 UTC |