In perl5, I used "do (file)" for configuration files as follows:
---script.pl start ---
our @conf = ();
do '/path/some_conf_file';
...
foreach $item (@conf) {
$item->{rules} ...
...
---script.pl end ---
---/path/some_conf_file start ---
@conf = (
{name => 'gateway',
rules => [
{verdict => 'allow', srcnet => 'gw', dstnet => 'lan2'}
]
},
{name => 'lan <-> lan2',
rules => [
{srcnet => 'lan', dstnet => 'lan2',
verdict => 'allow', dstip => '192.168.5.0/24'}
]
},
);
---/path/some_conf_file end ---
Also Larry Wall "Perl Programming" also mentions this method:
But FILE is still useful for things like a reader, configuration files. Manual error checking can be performed as follows:
for $file ("/usr/share/proggie/defaults.rc",
"$ENV{HOME}/.someprogrc") {
unless ($return = do $file) {
warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!" unless defined $return;
warn "couldn't run $file" unless $return;
} }
Benefits
- no need to write your own parser every time - perl parse and creating data structures for you;
- faster / easier: own data perl structures / types without overhead for conversion from an external format (for example, YAML);
- does not require manipulation of @INC to load a module somewhere compared to a module as a conf file;
- less code compared to modules as a conf file;
- "" " " perl;
- "ad hoc" ;
perl6?
perl6 ( ) , , , ?
- " "?