Config-Access-Driver

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN


if ( $config_file->getErrorCode() == 0 ) {
    my $server_section = $config->getConfigSectionbyName($server_prefix);

    if ( defined $server_section ) {

        #------------------------
        #Server Backup Configuration

        #Value with fallback default in one call
        $backup_directory = $server_section->get( 'BACKUPDIR', $backup_directory );

        #Existence check without hash plumbing
        if ( $server_section->hasKey('SAVEDAYS') ) {
            $save_days = $server_section->get('SAVEDAYS');
        }
        else    #The Server Configuration is not complete
        {
            $error_message .=
              "Server '$server_prefix': Server is not completely configured.\n"
              . "Assuming SAVEDAYS = '$save_days'\n";

README.md  view on Meta::CPAN


```perl
my $config = Config::Access::Driver::readConfigSectionList('/path/to/config.ini');

#Fast indexed lookup by section name
my $db_config = $config->getConfigSectionbyName('database');
```

### Comparison with Config::IniHash

The same task — read a backup configuration, validate required keys — written with `Config::IniHash`:
```perl
$config = ReadINI $config_file_path;

if ( defined $config ) {
    if ( defined $config->{$server_prefix} ) {

        if ( defined $config->{$server_prefix}->{'BACKUPDIR'} ) {
            $backup_directory = $config->{$server_prefix}->{'BACKUPDIR'};
        }

        if ( defined $config->{$server_prefix}->{'MAILTO'} ) {
            $smailto = $config->{$server_prefix}->{'MAILTO'};
        }
        else    #The Server Configuration is not complete
        {
            $error_message .=
              "Server '$server_prefix': Server is not completely configured.\n";
        }
    }
}
```

Every access **repeats the full hash-of-hashes path** — `$config->{$server_prefix}->{'KEY'}` —
wrapped in exists guards.
Reconstructing numerically keyed options into an array gets worse:
```perl
if ( exists $config->{ $server_prefix . $backup_plan_section } ) {
    foreach ( keys %{ $config->{ $server_prefix . $backup_plan_section } } ) {
        $#backup_plan = $_ if ( $_ + 1 > @backup_plan );
        $backup_plan[$_] = $config->{ $server_prefix . $backup_plan_section }->{$_};
    }
}
```

With `Config::Access::Driver`, the same intent becomes named, typed method calls:
```perl
$backup_directory = $server_section->get( 'BACKUPDIR', $backup_directory );

if ( $server_section->hasKey('MAILTO') ) { ... }
```

The difference compounds in real applications: every hash-path access in the `Config::IniHash` version
is a potential `undef` dereference and must be guarded individually, while the object API concentrates
those checks into **get defaults**, `hasKey()`, and the **driver's Error Code**.

## Where the trade-off lies



( run in 3.720 seconds using v1.01-cache-2.11-cpan-a49fcb8fa48 )