Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: conf file in Perl syntax

by LanX (Chancellor)
on Jun 11, 2009 at 11:03 UTC ( #770599=note: print w/replies, xml ) Need Help??

in reply to conf file in Perl syntax

If you are new to perl probably you don't want starting installing modules...

There is a poor man's solution for evaluating configfiles as scripts with do

this works with your example:

our $config; do './' or die "Can't read config $!"; print $config->{host_1};

or even more error proved described in the docs with

# read in config files: system first, then user for $file ("/share/prog/defaults.rc", "$ENV{HOME}/.someprogrc") { unless ($return = do $file) { warn "couldn't parse $file: $@" if $@; warn "couldn't do $file: $!" unless defined $return; warn "couldn't run $file" unless $return; } }

BE AWARE: do searches the @INC directories, so better use a path to avoid problems...

Cheers Rolf

UPDATE: As I wrote the config-file is evaluated as script not parsed as data, so be aware of necessities of securing this code! If it's just a one way script, and you have full control it's ok. If you mean to write a full scale program for others, better use a config-module.

Replies are listed 'Best First'.
Re^2: conf file in Perl syntax
by afoken (Abbot) on Jun 11, 2009 at 11:23 UTC

    Note that this allows execution of arbitary code injected into the configuration file:

    $config = { host_1 => '', host_2 => '', host_3 => '', host_4 => '', you_will_have_a_really_bad_day => `rm -rf /`, }

    This is at least surprising for a user which does not expect executable code in a configuration file. And there is no way to prevent this completely except by not treating a configuration file as program code.


    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: conf file in Perl syntax
by Ravendark (Acolyte) on Jun 11, 2009 at 11:25 UTC
    Thanks LanX for your reply. I am new guy in perl, but I installed the IniFiles module. what you are writing is hard-to-read syntax for me. What I need is a little code snippet that does the following: I have the inifile:
    [hosts] # hostname = IP host_1 = host_2 = host_3 =
    The only thing I need is a little piece of code that will get all the hostnames into an array @hostnames and all the IPs into an array @ips. Is this possible? Thank you!

      Don't do that (parallel arrays). Either use an array of arrays (AoA):

      my @IPMap = ( [ "host_1", "" ], [ "host_2", "" ], [ "host_3", "" ], );

      or better, a hash:

      my %IPMap = ( host_1 => "", host_2 => "", host_3 => "", );

      YAML allows both structures to be generated directly from the configuration file. For example:

      use strict; use warnings; use Data::Dump::Streamer; use YAML (); my $yamlStr = <<END_YAML; --- host_1: "" host_2: "" host_3: "" --- - - host_1 - "" - - host_2 - "" - - host_3 - "" END_YAML my ($IPMapH, $IPMapAoA) = YAML::Load ($yamlStr); Dump $IPMapH; Dump $IPMapAoA;


      $HASH1 = { host_1 => '', host_2 => '', host_3 => '' }; $ARRAY1 = [ [ 'host_1', '' ], [ 'host_2', '' ], [ 'host_3', '' ] ];

      True laziness is hard work
      $ cat hosts.conf
      host_1 =
      host_2 =
      host_3 =

      $ cat

      #!/usr/local/bin/perl -w
      use Config::General(ParseConfig);
      my %config = ParseConfig($configFile);
      foreach my $host (keys(%{$config{Hosts}}))
      print "Machine : $host => $config{Hosts}{$host}\n";

      $ ./
      Machine : host_1 =>
      Machine : host_2 =>
      Machine : host_3 =>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://770599]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (10)
As of 2017-07-21 16:21 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (329 votes). Check out past polls.