tariqahsan has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to assign a variable with a value read from a config file. Also, it could be a config file that could be sourced to set the ENV and getting the value from it within the script. I tried to use AppConfig and Config::IniFiles but without any useful success. I also tried the following like snippet:

%ENV = map {\^([^=]+)=(.*)$/} split /\n/, `. ./env.conf;env`; $param = $ENV{'PARAM'}; print " PARAM = $param\n";

Haven't had any useful success. Anybody could help?

edited by footpad, ~Fri Nov 30 21:09:39 2001.

Replies are listed 'Best First'.
(jeffa) Re: How to assign a variable with a value read from a config file
by jeffa (Bishop) on Dec 01, 2001 at 02:17 UTC
    Without seeing what is inside env.conf - i can only guess that PARAM will be there. But you have other issues - you are never opening a file handle for env.conf -
    open(FILE,'../env.conf') or die "not there";
    now you can either slurp the contents into an array or a scalar, or iterate through the file one line at a time. Small files can use the former technique without a hitch, but the latter technique is better for huge files. Since you are splitting on new lines - let's use a scalar:
    my $content = do {local $/; <FILE>};
    Next, don't use %ENV - use your own hash, how about
    my %conf = map { /^([^=]+)=(.*)$/; $1 => $2 } split /\n/, $content;
    but that doesn't quite work - if it reads '=bar' then you will have an undefined key that points at the value 'bar'. map is fine and dandy for data transformation, but as soon as the logic inside the code block gets tricky, i like to find another way. Let's try a while loop on an open file handle:
    my %conf; while (<FILE>) { chomp; my ($k,$v) = $_ =~ /^([^=]+)=(.*)$/; $conf{$k} = $v if $k; }
    Hope this helps!

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
Re: How to assign a variable with a value read from a config file
by dragonchild (Archbishop) on Dec 01, 2001 at 02:17 UTC
    You're asking two questions.
    1. Q: How do I read a value from a file and assign it to a variable.

      A: Look in the Q&A section.

    2. Q: How do I read a value that is in my environment and assign it to a variable.

      A: Look in the Q&A section. Or, read a Perl manual.

      Or, cause I'm nice, try my $param = $ENV{PARAM}; ... Perl is nice and puts the entire environment into %ENV for you.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: How to assign a variable with a value read from a config file
by perrin (Chancellor) on Dec 01, 2001 at 04:21 UTC
    Did you want to use AppConfig, Confgi::IniFiles, or some other CPAN module? If so, post what went wrong when you tried to use it so that people can offer help.
Re: How to assign a variable with a value read from a config file
by WildThing (Initiate) on Dec 01, 2001 at 14:09 UTC
    Maybe I'm over simplifying but even though that other answers here are good solutions, it looks from your post you don't care about the vars being global and want the varname used in the config file to be the scripts varname

    Just make the format of your config file as follows:
    $VARNAME="value";

    One define on each line and be sure the last line is '1;' without the quotes.

    Add the following line to your script -
    require './configfile';

    If the directory of the configfile is not the same as the script the substitute the ./ for the directory name i.e. /tmp/

    Why didn't I take the Blue Pill