Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Configuration File

by legLess (Hermit)
on Jul 15, 2003 at 03:47 UTC ( [id://274269]=note: print w/replies, xml ) Need Help??


in reply to Configuration File

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://274269]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-18 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found