Ken
Ken

Reputation: 4163

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.

Upvotes: 1

Views: 1229

Answers (2)

williamt
williamt

Reputation: 473

A little late to the party but I found this works if you don't want to predefine everything.

use warnings;
use strict;

use AppConfig ':argcount';

my $config = AppConfig->new( {
    CREATE => 1,
    GLOBAL => { ARGCOUNT => ARGCOUNT_ONE },
  } );
$config->file('sample.cfg');

print $config->foo();
# or
print $config->get('foo');

Upvotes: 0

TLP
TLP

Reputation: 67900

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";

Upvotes: 2

Related Questions