Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

(TIMTOWDI) Breaking a config file into name/value pairs

by stonecolddevin (Vicar)
on Sep 03, 2004 at 03:52 UTC ( #388201=snippet: print w/replies, xml ) Need Help??
Description: 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:
### 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()
    # 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 
    return %values;
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://388201]
[erix]: I recognise the makings of a fine argument
[LanX]: lanx wonders ... how likely is it to talk >95% BS without intention?
[erix]: "gigantic amounts of data" is also not SQLite (imho)
talexb wonders why sqlite2 was deprecated in favour of sqlite3.
[erix]: looks like a fork, rather, no?
LanX /me /me
[erix]: /hehehe
[marto]: LanX yesterday I found out about Gish gallop tactic
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2017-07-28 15:38 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (431 votes). Check out past polls.