Config::Any; great module, Bad Documentation

28 Feb    Uncategorized
Bad documentation!

Config::Any provides a facility for Perl applications and libraries to load configuration data from multiple different file formats. It supports XML, YAML, JSON, Apache-style configuration, Windows INI files, and even Perl code.

Pretty nice module to read up configs quickly if you are into perl, but the lack of detail explanation for this is incredible, the author claims that using cpan is to actually help you save time in development rather than code a new thing by yourself, as this might save others the trouble of having to go through more code..

(WordPress is breaking my tabs and space in the code.)

UNFORTUNATELY, by the time one read up that documentation and figures out how to read a simple configuration file, he could have just coded up a simple perl script to read his configuration file easily.  E.g Without Config::Any one can do something as quick as this:

#!/usr/bin/perl
use warnings;
use strict;
my $cfgFile = "./master.conf";
my %bconfig;
sub fetchConfig()
{
my $line="";
open my $cfg, "<", $cfgFile or die "Cannot open config file $cfgFile\n";
while ($line=<$cfg>) {
chomp($line);
if ( $line=~/^#/ && $line=~/^\s/) {next; }
elsif ($line=~/^([^=]+)=(.+)$/) {
$bconfig{"$1"}="$2";
}
}
close($cfg);
}
fetchConfig();
print $bconfig{'mysql_login'}."\n";
print $bconfig{'mysql_password'}."\n";

and then how one can do it with Config::Any.


#!/usr/bin/perl
use warnings;
use strict;
use Config::Any;
use Data::Dumper;
my $t='./master.conf';
my $cfgFile = qq{$t};
my @cfgFileList = ($cfgFile);
my $cfg = Config::Any->load_files({files => \@cfgFileList, use_ext => 1});
print $cfg->[0]{$cfgFile}{mysql_login};

If you want to see the structure of your data you can run this from your shell:
perl -Mstrict -Mwarnings -E '
use Config::Any;
use Data::Dumper;
my $cfgFile = q{./master.conf};
my @cfgFileList = ($cfgFile);
my $cfg = Config::Any->load_files({files => \@cfgFileList, use_ext => 1});
say q{Full $cfg structure:};
say Dumper $cfg;
say q{Password: }, $cfg->[0]{$cfgFile}{mysql_login};
'

Really, writing great software is in no way an excuse not to write proper documentation. If you can’t write example of usage for your piece of software, it is as good as telling someone to just write up his own version. Because bad documentation CONSUMES time. . . and remember, always give example of usage.

Filters
Hmm sometimes you may want to strip out stuffs from your data you picked up from the config file, e.g stripping out single quotes (yes if you have double quotes in the values, it is ignored when read, but single quotes are weirdly kept.)
#!/usr/bin/perl
use warnings;
use strict;
use Convert::UU qw(uudecode uuencode);
use Config::Any;
my $CONFIGFILE='./master.conf';
sub readConfig
{
my $cfgFile=qq{$CONFIGFILE};
my @cfgFileList = ($cfgFile);
my $cfg = Config::Any->load_files({files => \@cfgFileList, filter =>\&doWhateverFilter, use_ext => 1});
return $cfg;
}
sub doWhateverFilter
{
use Data::Dumper; warn Dumper(\@_);
}
readConfig();

# As you can see we are are calling doWhateverFilter when loading the config, this will jsut pass by reference our config hash. In the above example I have used data dumper so as you can have an idea what the structure looks like, you can write anything in the doWhateverFilter to change the content of that structure hence directly affecting $cfg.

🙂 to MST, thank you for the great piece of software, and FUCK YOU for the bad documentation so bad that it is better to just go and look at the source and figure out rather than reading the documentation :p.

Yes I’d pay you a beer any time though.

+selven

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.