in reply to Config files

maybe with eval
# cat test.ini [path] root = C:\test code = $root . '\code1' # cat test.pl use Config::IniFiles; $ini = Config::IniFiles->new(-file => 'test.ini'); $root = $ini->val('path', 'root'); print "root = $root\n"; $code = $ini->val('path', 'code'); print "code = $code\n"; $code = eval $code; print "code = $code\n"; # perl test.pl root = C:\test code = $root . '\code1' code = C:\test\code1
note that I had to change your double quotes to single quotes in the code ini file entry.

Replies are listed 'Best First'.
Re^2: Config files
by Jenda (Abbot) on Dec 07, 2004 at 01:53 UTC

    I don't think it's wise to give the users so much power. Not only would eval"" let them run whatever code they want inside the application (which may or may not matter much) but it can also easily break the app in ways that will not be aparent to the user.

    Besides you will want them to be able to reuse whatever option they like, not just the "root". So they would either have to use

    [path] root = C:\test code = $ini->val('path', 'root') . '\code1'
    or something similar or you'd have to copy all the values into whatever package the eval""ed code runs in. Which is again not something you want to do.

    It's (IMnsHO) better to invent a special, more restricted syntax and parse it instead of eval""ing.

    Jenda
    We'd like to help you learn to help yourself
    Look around you, all you see are sympathetic eyes
    Stroll around the grounds until you feel at home
       -- P. Simon in Mrs. Robinson

Re^2: Config files
by sparkel (Acolyte) on Dec 06, 2004 at 23:48 UTC
    Hi,

    Thanks for the reply. Is there an easier way to use substitutions like I have used above. Something easy that people who don't know perl ( that is don't know what '$' or '.' is, can update, append or change the config file)

    Since I am repeating a lot of path names, I wanted to substitute those variables throughout the config file, so inorder to change all of them, only one change would be required.

    Thank you.