These snippets illustrate the following:
Configuration data is aggregated in hashes (just one for these snippets) to cut down on the number of variable names.
The configuration hashes are maintained in their own namespace to avoid collisions.
Configuration files are maintained in Perl syntax for easy reading, writing and syntax verification.
By maintaining config data in a hash, you can not only quickly access key-value pairs, but also maintain more complex data. For example,
%CFG = ( 'servers' => { 'SRV1' => { 'IP' => 99.32.4.0, 'user' => 'aname', 'pswd' => 'p4ssw0rd', 'status' => 'unavailable' }, 'SRV2' => { 'IP' => 129.99.10.5 'user' => 'guest', 'pswd' => 'guest' 'status' => 'unavailable' } }, 'timeout' => 60, 'log' => { 'file' => '/var/log/my_log.log', 'level' => 'warn', }, 'temp' => 'remove me' );
Maintaining config data in Perl syntax makes it easy to read:
# Read a configuration file # The arg can be a relative or full path, or # it can be a file located somewhere in @INC. sub ReadCfg { my $file = $_[0]; our $err; { # Put config data into a separate namespace package CFG; # Process the contents of the config file my $rc = do($file); # Check for errors if ($@) { $::err = "ERROR: Failure compiling '$file' - $@"; } elsif (! defined($rc)) { $::err = "ERROR: Failure reading '$file' - $!"; } elsif (! $rc) { $::err = "ERROR: Failure processing '$file'"; } } return ($err); } # Get our configuration information if (my $err = ReadCfg('my_cfg.cfg')) { print(STDERR $err, "\n"); exit(1); }
The config data is easy to access in its own namespace:
# Add last access time $CFG::CFG{'servers'}{'SRV1'}{'last_access'} = time(); # Change server availablility $CFG::CFG{'servers'}{'SRV1'}{'status'} = 'online'; # Delete temporary data delete($CFG::CFG{'temp'}); # Open log file my $LOG; if (! open($LOG, "> $CFG::CFG{'log'}{'file'}")) { print(STDERR "ERROR: Failure opening log file: $!\n"); exit(1); }
The Data::Dumper module lets you write config data easily:
use Data::Dumper; # Save configuration data # Use the same arg as used with ReadCfg() # so that file can be found in the %INC. sub SaveCfg { my $file = $INC{$_[0]}; my $CFG; if (! open($CFG, "> $file")) { return ("ERROR: Failure opening '$file' - $!"); } print $CFG <<_MARKER_; ##### # # My configuration file # ##### use strict; use warnings; our (%CFG); # The configuration data @{[Data::Dumper->Dump([\%CFG::CFG], ['*CFG'])]} 1; # EOF _MARKER_ close($CFG); return (undef); # Success } # Save our configuration file if (my $err = SaveCfg('my_cfg.cfg')) { print(STDERR $err, "\n"); exit(1); }
The syntax of the config file can be easily checked using:
perl -c my_cfg.cfg

In reply to Reading and Writing Perl Config Files by jdhedden

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.