This script simply takes a file in the "name=value" format, and breaks it into a hash for one to use as they please.
UPDATE: Due to my blindness and ignorance to search CPAN, I have discovered just now there is a module that does this EXACT thing. Damn.
Some links to CPAN mods of the same nature:
Config::Ini
Config::Tiny
### returns the requested setting
### Used like so: my $name = getSetting('name');
### Config file: name=Devin
sub getSetting {
my ($attr_to_get) = shift; # the attribute to get
# opens and reads the config file,
# returns a hash
my %attrs = openCfgFile()
('your/path/to/config.file');
# return the requested attribute if it exists
# otherwise, give them a big fat null
return $attrs{$attr_to_get}
? $attrs{$attr_to_get}
: "NULL";
}
### open, read, and return the hash.
sub openCfgFile {
my ($cfg) = shift; # the file parameter
# open and read
open CFG, "<", $cfg or die "File not found: $!";
my @settings = <CFG>;
close CFG;
my %values; # create the values hash
foreach (@settings) { # loop through the file
chomp; # remove endline characters (\n, \r
+....)
s|#.+||; # remove comments
s|"(.+?)"|$1|g; # (not working) allow whitespace fo
+r strings
s|\s||; # remove whitespace
my( $key, $val) = split /=/; # begin creating the hash, split
+ the file on the "=" sign
$values{$key}=$val; # create the hash key/values
+pairs
}
return %values;
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.