| 1 | package Config::Lite; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use Fcntl qw/:flock/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 2 | require Exporter; | |||||||||
| 8 | |||||||||||
| 9 | 2 | our @ISA = qw(Exporter); | |||||||||
| 10 | 2 | our @EXPORT_OK = qw(load_config set_config_separator); | |||||||||
| 11 | 2 | our @EXPORT = qw(); | |||||||||
| 12 | 2 | our $VERSION = '0.03'; | |||||||||
| 13 | |||||||||||
| 14 | 2 | our %separator; | |||||||||
| 15 | 2 | $separator{'kv'} = "="; | |||||||||
| 16 | 2 | $separator{'comment'} = "#"; | |||||||||
| 17 | 2 | $separator{'line'} = "\n"; | |||||||||
| 18 | |||||||||||
| 19 | sub set_config_separator | ||||||||||
| 20 | 3 | ▶ | { | ||||||||
|
Subroutine
set_config_separator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 21 | 3 | my $kind = shift; | |||||||||
| 22 | 3 | my $str = shift; | |||||||||
| 23 | 3 | $separator{$kind} = $str; | |||||||||
| 24 | 3 | return 1; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | sub load_config | ||||||||||
| 28 | 4 | ▶ | { | ||||||||
|
Subroutine
load_config: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 29 | 4 | my $config_filename = shift; | |||||||||
| 30 | 4 | ▶ | open my $fh,"<", $config_filename or return "no config file found"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 2 | flock $fh, LOCK_SH; | |||||||||
| 32 | 2 | my $config_string = do { local $/ ; <$fh> }; | |||||||||
| 33 | 2 | flock $fh, LOCK_UN; | |||||||||
| 34 | 2 | close $fh; | |||||||||
| 35 | |||||||||||
| 36 | 2 | my @config_array = split /$separator{'line'}/, $config_string; | |||||||||
| 37 | 2 | my %config_hash; | |||||||||
| 38 | 2 | foreach ( @config_array ) | |||||||||
| 39 | { | ||||||||||
| 40 | 22 | ▶ | next unless $_ =~ /$separator{'kv'}/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 13 | ▶ | next if $_ =~ /^$separator{'comment'}/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 12 | my ($k, $v) = $_ =~ /\s*(.+?)$separator{'kv'}(.+?)\s*$/; | |||||||||
| 43 | 12 | $config_hash{$k} = $v; | |||||||||
| 44 | } | ||||||||||
| 45 | 2 | return %config_hash; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | 2 | 1; | |||||||||
| 51 | __END__ | ||||||||||