Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I'm having trouble with the above module/method.
Here is the header from the module:

package Config::Simple; # $Id: Simple.pm,v 3.50 2003/04/29 01:42:25 sherzodr Exp $ use strict; # uncomment the following line while debugging. Otherwise, # it's too slow for production environment #use diagnostics; use Carp; use Fcntl qw(:DEFAULT :flock); use Text::ParseWords 'parse_line'; use vars qw($VERSION $DEFAULTNS $LC $USEQQ $errstr); use AutoLoader 'AUTOLOAD'; $VERSION = '4.55'; $DEFAULTNS = 'default';

I'm confused as to the ver num, it seems to be 3.50 and 4.55 ???

My code is this:

#!/usr/bin/perl -w + use Config::Simple; use strict; + + my $cfg; my %config; $cfg = new Config::Simple('test.cfg'); open(FH,"test.cfg"); printf("'%s' syntax\n", $cfg->guess_syntax(\*FH)); close FH; %config = $cfg->vars(); print "NET = $config{NET} \n"; exit;

and my cfg file is a 1 liner (for test purposes):

NET = X

The output is:

'ini' syntax Use of uninitialized value in concatenation (.) or string at ./t2.pl l +ine 14. NET =

which is odd because it should be 'simple' syntax according to the CPAN page http://search.cpan.org/~sherzodr/Config-Simple-4.55/Simple.pm

More importantly, it doesn't return the cfg file value. I've had a look through google and here (perlmonks) but can't find the problem mentioned. Maybe it's just me.... Any help appreciated.
I'd really like to use the hash syntax, because it's simpler/less 'noisy' than the other options.

Cheers
Chris
PS Supplementary dumb qn: why do i have to use HTML tags writing my qn. Why won't it accept CR/LF chars as in normal text?

Replies are listed 'Best First'.
Re: Config::Simple with vars method
by davidj (Priest) on Jul 06, 2004 at 04:55 UTC
    First, Data::Dumper reveals why you are not getting anything returned.

    #!/usr/bin/perl -w use Config::Simple; use strict; use Data::Dumper; my $cfg; my %config; $cfg = new Config::Simple('test.cfg'); open(FH,"test.cfg"); printf("'%s' syntax\n", $cfg->guess_syntax(\*FH)); close FH; %config = $cfg->vars(); print Dumper(\%config); exit; D:\PerlProjects\tests>c.pl
    output:
    'ini' syntax $VAR1 = { 'default.NET' => 'X' };
    Now the reason for it (from a cursory reading of the Config::Simple manpage):
    Config::Simple assumes config information is formatted in a block system, such as:
    [block1] key=value
    Since your config file does not have any block delimiters (indicated by brackets), the values are assigned to the 'default' block.
    changing your config file to the following:
    [info] NET=X
    generates the following output:
    D:\PerlProjects\tests>c.pl 'ini' syntax $VAR1 = { 'info.NET' => 'X' };
    I would suggest you perldoc Config::Simple to learn more about the module.

    hope this helps,
    davidj
      David,
      I follow your info, but the CPAN page says: Quote
      Return value is one of "ini", "simple" or "http".
      Unquote
      when mentioning the 'guess_syntax' method. As my file is simple, it should recognise it as such.
      It also shows what code to use for 'simple' and 'ini' style files: Quote
      my %Config = $cfg->vars(); print "Username: $Config{User}"; # If it was a traditional ini-file: print "Username: $Config{'mysql.user'}";
      where the first is for 'simple' files, which I want to use.
      Cheers
      Chris
        Chris,
        I think you need to read the documentation a little more closely. It reads:

        SIMPLE CONFIGURATION FILE

        "Simple syntax is what you need for most of your projects. These are, as the name asserts, the simplest. File consists of key/value pairs, delimited by nothing but white space."

        INI-FILE

        "These configuration files are more native to Win32 systems. Data is organized in blocks. Each key/value pair is delimited with an equal (=) sign. Blocks are declared on their own lines enclosed in (brackets):"

        Your not providing blocks does not make it a 'simple' file. The fact that you are using '=' as a key/value delimiter does make it an 'ini' file. If you want to use 'simple', the replace the '=' with whitespace.

        davidj
Re: Config::Simple with vars method
by matija (Priest) on Jul 06, 2004 at 07:02 UTC
    # $Id: Simple.pm,v 3.50 2003/04/29 01:42:25 sherzodr Exp $ ... $VERSION = '4.55';
    I'm confused as to the ver num, it seems to be 3.50 and 4.55 ???

    3.50 is his CVS version, which is completely irrelevant in this case. There is no reason why the author should make a release every time he makes a minor change in his code - indeed in most cases, he will make many changes and many CVS commits between releases.

    The automated tools most developers use to make CPAN releases quite correctly ignore the CVS version, and only take into account the $VERSION setting.

    Therefore 4.55 is the correct version.