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

I am using AppConfig in a program as it handles both command-line and configuration files for program options in one interface. The problem I am having is using the ERROR handler provided with AppConfig. The only samples of code I have looked at look like this:
my $config = AppConfig->new( { CASE => 1, ERROR => \&my_error, GLOBAL => { DEFAULT => "<unset>", ARGCOUNT => ARGCOUNT_ONE, }, } );
I thought I would use a subroutine here but that does not allow AppConfig to display its errors.
my $config = AppConfig->new( { CASE => 1, ERROR => hanlde_errors(), GLOBAL => { DEFAULT => "<unset>", ARGCOUNT => ARGCOUNT_ONE, }, } );
Basically, I would like AppConfig to display its errors, print the pod2usage documentation, and exit. I will admit I do not understand what AppConfig is doing with the \&my_error and not been able to figure it out.

Does any have examples of this? I have done searches here and used google but not much luck.

Thanks sxmwb

Replies are listed 'Best First'.
Re: AppConfig Error handling
by pc88mxer (Vicar) on May 04, 2008 at 15:06 UTC
    The ERROR parameter is a custom subroutine for displaying errors from AppConfig. If not specified, errors are displayed via warn. Here's an example of a custom error handler:
    sub handle_errors { # prefix error message with current time my $message = sprintf(@_); my $time = localtime(time); warn "$time: $message\n"; }
    And, as FunkyMonk points out, you would tell AppConfig to use this handler with:
    my $conf = AppConfig->new({ ..., ERROR => \&handle_errors, ...});
    Update: Thanks to FunkyMonk for pointing out the initial typo I had with the the subroutine's name.
Re: AppConfig Error handling
by FunkyMonk (Bishop) on May 04, 2008 at 14:45 UTC
    Well, I don't know AppConfig, but, from what you've posted, ERROR needs a subref (that's what "\&my_error" is -- a reference to a subroutine called my_error).

    What you've used is a subroutine call. You need to change it to ERROR  => \&hanlde_errors, (note: OP's typo untouched) assuming you've got a sub hanlde_errors { ... } somewhere.


    Unless I state otherwise, all my code runs with strict and warnings
Re: AppConfig Error handling
by sxmwb (Pilgrim) on May 05, 2008 at 22:39 UTC
    Thanks for pointing out the typo, I need to slow down a bit and verify things.

    Thanks for the information on the \&my_error and the sample code. I was not understanding how that worked.

    Mike

    Update