http://www.perlmonks.org?node_id=802011

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

Hi,

how to read variables from conf file.

exp: auto.conf my a = 1000 my b = "test" my c = 2000 my d = "test2"
############################### test.pl ############################### #!/usr/bin/perl require auto.conf (can i use require)

Replies are listed 'Best First'.
Re: how to read conf file in perl script
by bv (Friar) on Oct 19, 2009 at 15:23 UTC

    See Config::Simple or search Config

    print pack("A25",pack("V*",map{1919242272+$_}(34481450,-49737472,6228,0,-285028276,6979,-1380265972)))

      I also find Config::Wrest pretty useful, plus it comes from the BBC, though i realise this isn't much of a plaudit for non-brits...

      Just a something something...
Re: how to read conf file in perl script
by darkphorm (Beadle) on Oct 19, 2009 at 21:14 UTC
    If you're looking to use a fairly standard formatted config file (rather than one with "my" variables), but instead something along the lines of
    [mysection] somevar = value
    You might try Config::IniFiles
    use Config::IniFiles; my $config_data = new Config::IniFiles( -file => $config_file ) or die +("Cannot read $config_file"); $myvar = $config_data->val('mysection', 'somevar', 'defaultvalue');
Re: how to read conf file in perl script
by cdarke (Prior) on Oct 19, 2009 at 16:26 UTC
    Good news: require can be used to read such a file

    Bad news: my (lexical) variables will not be inherited by the caller. Use package variables, see our.
      even worse news, that file is not valid perl, so that won't work.
        Cange your config file to a module - eg.
        package MyApp::Config; our $a = 1000; our $b = "test"; our $c = 2000; our $d = "test2"; 1;
        Then you can load it with use MyApp::Config; and access variables in following way $MyApp::Config::c.

      Really worse old news: requireing configuration files opens a gapping security hole. Don't do that!

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        CPAN does it, CPANPLUS does it, even schwern does it :p
Re: how to read conf file in perl script
by ph0enix (Friar) on Oct 20, 2009 at 08:07 UTC