Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

how to read conf file in perl script

by hardikv (Initiate)
on Oct 19, 2009 at 15:12 UTC ( #802011=perlquestion: print w/ replies, xml ) Need Help??
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)

Comment on how to read conf file in perl script
Select or Download Code
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 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 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 ph0enix (Friar) on Oct 20, 2009 at 08:07 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://802011]
Approved by wfsp
Front-paged by biohisham
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-08-02 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls