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.
In reply to Dynamic variables and strict by solitaryrpr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |