0: # What makes this one better? nothing really. i tried out
1: # App::Config, but (according to my understanding) one needs
2: # to state all variables in the configuration before reading
3: # in the configuration file. That's useful in some situations,
4: # i just didn't like it. i tried Config::IniFile which worked
5: # alright, but being lazy i don't like a function call in
6: # between me and my values (i could also tie a hash to it,
7: # which is useful, but i had a couple minutes so i thought i'd
8: # do a quick reinvention of the wheel)
9: #
10: # What makes mine different is that it's a quick and dirty parser
11: # that floods the packages namespace (not main's) with the
12: # variables. It doesn't handle arrays or hashes (to be handled
13: # at a later date) just simple name=val (with any number of
14: # spaces).
15: #
16: # The way to call it (in case you're interested) is just:
17: #
18: # use ConfFile("conf/file/name_here");
19: #
20: # this seems elegant and useful because i can put in one line
21: # at the top of my code and use all of my configuration
22: # variables. It just makes sense to me for some reason.
23: # Anyway, enough banter, here it is...
24:
25: #!/usr/local/bin/perl -w
26:
27: package ConfFile;
28: use strict;
29: use Carp;
30:
31: sub import {
32: shift; # this gets rid of the calling package...
33: my ($call) = caller;
34:
35: # i also changed this: why die cuz there was no file?
36: my $filename = $_[0] || return;
37: my $symbols = &readFile;
38:
39: carp "No values were read in from $filename.\n" unless (keys %$symbols);
40:
41: # one has to turn off strict 'refs' in order to flood the namespace of
42: # the calling package in this way.
43: no strict qw(refs);
44: foreach (keys %$symbols) {
45: *{"${call}::${_}"} = \$symbols->{$_};
46: }
47: use strict qw(refs);
48:
49: return;
50: }
51:
52: sub readFile {
53: my $file = shift;
54: my (%sym, $key, $val);
55: local $_;
56:
57: open FILE, $file or croak "Couldn't open file $file: $!";
58:
59: while (<FILE>) {
60: next if /^\s*#|;/;
61: next if /^$/;
62: chomp;
63:
64: ($key, $val) = /^\s*?(\S+)\s*?=?\s*?(\S+)$/;
65: $sym{$key} = $val;
66: }
67: close FILE;
68:
69: return (keys %sym) ? \%sym : undef;
70: }
71:
72:
73: 1;
74:
75: # please, please comment on anything done badly, i do hope
76: # to use this someday soon...
77: #
78: # jynx
79: #
80: # Update: i used Fastolfe's suggestions so this looks
81: # a little better... In reply to Yet Another Config File Parser Module by jynx
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |