The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Config::Autoload - Autoloads the config file whenever it is changed

VERSION

Version 0.01

SYNOPSIS

    use Config::Autoload;

    my $config = Config::Autoload->new("/path/sample.conf");
    my $value = $config->load_key('key');

    # or

    my $config = Config::Autoload->new("/path/sample.conf",\&construct);
    my $value = $config->load_key('key');

    sub construct {

        my $file = shift;
        my %hash;

        open FD,$file or die $!;
        while(<FD>) {
            next if /^#|^$/;
            chomp;
            my ($k,$v) = split/ = /,$_;
            $hash{$k} =$v;
        }
        close FD;

        \%hash;
    }

METHODS

new()

Create an object. The full path to the config file is required.

    my $config = Config::Autoload->new("/path/sample.conf");

If the second argument is ignored, new() uses a defalut construct() to built up a hash for storing the keys/values in the config file.

The default config file should be like:

    host 192.168.1.100
    port 1234
    user guest
    pass mypasswd

If you are using the different style of config file, you could built up your own construct() and pass it to the new() method.

For example, the config file looks like:

    host = 192.168.1.100
    port = 1234
    user = guest
    pass = mypasswd

Then a construct() for it could be:

    sub construct {

        my $file = shift;  # you just shift it
        my %hash;

        open FD,$file or die $!;
        while(<FD>) {
            next if /^#|^$/;
            chomp;
            my ($k,$v) = split/ = /,$_;
            $hash{$k} =$v;
        }
        close FD;

        \%hash;  # requires a hashref to be returned
    }

And pass the reference of this subroutine to new():

    my $config = Config::Autoload->new("/path/sample.conf",\&construct);

load_key()

Load the value with a key from the config file.

    my $value = $config->load_key('key');

I primarily used this module for mod_perl, under which the object exists for long time. Whenever the config file was changed, load_key() method will get the updated value.

AUTHOR

Jeff Pang <pangj@arcor.de>

BUGS/LIMITATIONS

If you have found bugs, please send email to <pangj@arcor.de>

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Config::Autoload

COPYRIGHT & LICENSE

Copyright 2010 Jeff Pang, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.