Use YAML! YAML is a simple, human-readable data-serialization format. It can round-trip any Perl data structure (that anything else can parse, anyway -- nothing's perfect) with no loss, doesn't require evals, and is very easy to work with.

One thing YAML's implementors had in mind at the start was to make a better syntax for config files. YAML's brutally simple in this regard:

# snippet my $data = "anything you want"; DumpFile( "/path/to/file", $data ); my $newdata = LoadFile( "/path/to/file" ); # now $data == $newdata

The code below is long, but that's mostly data. It dumps the data twice with Data::Dumper, before and after a YAML read/write cycle, just to prove they're the same. Lastly is the data as represented by YAML - very clean and easy to read.

#!/usr/bin/perl use strict; use warnings; use YAML qw/ LoadFile DumpFile Dump /; use Data::Dumper; local $Data::Dumper::Indent = 1; use constant DIV => '-' x 20 . "\n"; my $routers = { 'routers' => { 'router1' => '192.168.1.2', 'router2' => '192.168.1.3', 'router3' => '192.168.1.4', 'router4' => '192.168.1.5', 'router5' => '192.168.1.6', } }; my $groups = { 'groups' => { 'group1' => [ 'router1', 'router5', ], 'group2' => [ 'router1', 'router2', 'router3', ], 'group3' => [ 'router2', 'router3', 'router4', ] } }; print "Dumping initial data...\n" . DIV; print Dumper $routers, $groups; print "Writing YAML file...\n"; DumpFile( "./yaml-test.conf", $routers, $groups ); print "Reading YAML file...\n"; ( $routers, $groups ) = LoadFile( "yaml-test.conf" ); print "Dumping YAML'd data...\n" . DIV; print Dumper $routers, $groups; print "Dumping YAML'd data with YAML...\n" . DIV; print Dump $routers, $groups;
And here's the output:
Dumping initial data... -------------------- $VAR1 = { 'routers' => { 'router5' => '192.168.1.6', 'router1' => '192.168.1.2', 'router3' => '192.168.1.4', 'router4' => '192.168.1.5', 'router2' => '192.168.1.3' } }; $VAR2 = { 'groups' => { 'group2' => [ 'router1', 'router2', 'router3' ], 'group1' => [ 'router1', 'router5' ], 'group3' => [ 'router2', 'router3', 'router4' ] } }; Writing YAML file... Reading YAML file... Dumping YAML'd data... -------------------- $VAR1 = { 'routers' => { 'router5' => '192.168.1.6', 'router1' => '192.168.1.2', 'router3' => '192.168.1.4', 'router4' => '192.168.1.5', 'router2' => '192.168.1.3' } }; $VAR2 = { 'groups' => { 'group2' => [ 'router1', 'router2', 'router3' ], 'group1' => [ 'router1', 'router5' ], 'group3' => [ 'router2', 'router3', 'router4' ] } }; Dumping YAML'd data with YAML... -------------------- --- #YAML:1.0 routers: router1: 192.168.1.2 router2: 192.168.1.3 router3: 192.168.1.4 router4: 192.168.1.5 router5: 192.168.1.6 --- #YAML:1.0 groups: group1: - router1 - router5 group2: - router1 - router2 - router3 group3: - router2 - router3 - router4

--
man with no legs, inc.

In reply to Re: Configuration File by legLess
in thread Configuration File by crackotter

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.