Ken Ken - 6 months ago 8
Perl Question

AppConfig::File retrieving values

I am trying to use AppConfig::File for handling a config file. However, I am always getting an empty value from the object. The following is the code:

my $state = AppConfig::State->new( {
CREATE=>1,
} );
my $cfgfile = AppConfig::File->new($state);
$cfgfile->parse('sample.cfg');
my $temp = $cfgfile->{foo};
print "foo value: $temp\n";


This is in the sample.cfg:

## comment
foo = me


What did I do wrong? Thanks in advance.

TLP TLP
Answer

The solution was to include the ARGCOUNT setting in the definition of the variable. Not sure how to do it dynamically from the file.

use warnings;
use strict;

use AppConfig ':argcount';
use AppConfig::File;

my $state = AppConfig::State->new( {
    CREATE=>1,
  } );
$state->define('foo', { ARGCOUNT => ARGCOUNT_ONE });
my $cfgfile = AppConfig::File->new($state);
$cfgfile->parse('sample.cfg');
print "state value: '", $state->foo(), "'\n";