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

I need some help with AppConfig. I emailed Andy Wardley but after a whole day he did not respond.

First off, I want to know if I should be using this module or App::Config instead? Second, I dont know why I am having so much trouble taking a configuration and turning it into a hash.

Here is my file (.appconfig)

[urth] User = metaperl Pass = xxxxxxx Host = urth.org RemoteDir = /home/metaperl/rsync Type = I

Here is my program:

use AppConfig; use Net::FTP::Common; use strict; my $config = AppConfig->new({CASE=>1}); $config->file('/Users/metaperl/.appconfig'); my %urth = $config->varlist('^urth_', 1); use Data::Dumper; die Dumper(%urth);

But unfortunately, my output from the program is rather odd:

[localhost:~/src/net_ftp_common/scripts] metaperl% perl rsync.pl urth_User: no such variable at /Users/metaperl/.appconfig line 2 urth_Pass: no such variable at /Users/metaperl/.appconfig line 3 urth_Host: no such variable at /Users/metaperl/.appconfig line 4 urth_RemoteDir: no such variable at /Users/metaperl/.appconfig line 5 urth_Type: no such variable at /Users/metaperl/.appconfig line 6 Died at rsync.pl line 10. [localhost:~/src/net_ftp_common/scripts] metaperl%

Replies are listed 'Best First'.
Re: AppConfig help requested
by rob_au (Abbot) on Feb 06, 2002 at 09:41 UTC
    The problem here is that you haven't defined the variables which should be appearing in the configuration file - With this module, you need to define each variable like this:

    my $config = AppConfig->new({ 'CASE' => 1 }); $config->define("foo");

    You can also set per-variable parameters for the configuration file in the following manner:

    $config->define("foo", { 'DEFAULT' => "localhost:8080", 'CMDARG' => [ "-p" ], 'ARGCOUNT' => 1 });

    This definition of configuration file variables is described in the documentation for this module under the heading "Defining Variables".

    In my previous usage of this module, I actually found this requirement for definition of each configuration file variable individually quite annoying and so wrote up a base class which I called Local::Config - an extension to App::Config.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: AppConfig help requested
by simon.proctor (Vicar) on Feb 06, 2002 at 08:53 UTC
    I don't know if this helps with your AppConfig question but if you are reading a config file of the form name = value then there is a recipe in the Perl cookbook to cover it. This is slightly different to the example you gave where you have [] in place. If your config is more like an ini file then you would be better using Parse::RecDescent. The code looks something like (this is untested :P):
    foreach (@lines) { next if (/^#/); if(/^\s*(\S+)\s*=\s*(\S+)\s*$/) { $self->[CONFIG]->{$1} = $2; } }
    This allows for comment lines beginning with a # and, in the case above its being stored in a class package (a blessed arrayref here). The file has been read elsewhere.

    Note, I'm not suggesting that you avoid AppConfig but you're request appeared urgent so I'm just suggesting alternatives.

    HTH
Re: AppConfig help requested
by Cepa (Acolyte) on Feb 06, 2002 at 08:08 UTC
    "I emailed Andy Wardley but after a whole day he did not respond."

    Could he have been busy doing other things?

    Cepa was here
      "I emailed Andy Wardley but after a whole day he did not respond."

      Could he have been busy doing other things?

      I emailed him and it took him weeks to respond. It seems things get filtered to his AppConfig folder and then don't get looked at for awhile. Bizee guy I guess :-)