Hello,

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

-sol

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.