| 1 | package Config::Backend::String; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 7 | 6 | my $class=shift; | |||||||||
| 8 | 6 | my $strref=shift; | |||||||||
| 9 | 6 | my $self; | |||||||||
| 10 | |||||||||||
| 11 | 6 | ▶ | if (ref($strref) ne "SCALAR") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 12 | 1 | die "Conf::String must be initialized with a reference to a string"; | |||||||||
| 13 | } | ||||||||||
| 14 | |||||||||||
| 15 | 5 | $self->{"ref"}=$strref; | |||||||||
| 16 | 5 | my $str=${$strref}; | |||||||||
| 17 | |||||||||||
| 18 | 5 | my @cfgs; | |||||||||
| 19 | 5 | my $k=0; | |||||||||
| 20 | 5 | my $i=0; | |||||||||
| 21 | 5 | my $N=length $str; | |||||||||
| 22 | 5 | ▶ | while ($i<$N) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 23 | 204 | ▶ | if (substr($str,$i,2) eq "\n%") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 14 | ▶ | if (substr($str,$i,3) ne "\n%%") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 10 | my $cfg=substr($str,$k,($i-$k)); | |||||||||
| 26 | 10 | $cfg=~s/\n%%/\n%/g; | |||||||||
| 27 | 10 | $k=$i+2; | |||||||||
| 28 | 10 | push @cfgs,$cfg; | |||||||||
| 29 | } | ||||||||||
| 30 | else { | ||||||||||
| 31 | 4 | $i+=1; | |||||||||
| 32 | } | ||||||||||
| 33 | } | ||||||||||
| 34 | 204 | $i+=1; | |||||||||
| 35 | } | ||||||||||
| 36 | 5 | ▶ | if ($str ne "") { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 4 | my $cfg=substr($str,$k,$N); | |||||||||
| 38 | 4 | $cfg=~s/\n%%/\n%/g; | |||||||||
| 39 | 4 | push @cfgs,$cfg; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 5 | for my $cfg (@cfgs) { | |||||||||
| 43 | 14 | my ($var,$val) = split /=/,$cfg,2; | |||||||||
| 44 | 14 | $self->{"cfg"}->{$var}=$val; | |||||||||
| 45 | } | ||||||||||
| 46 | |||||||||||
| 47 | 5 | bless $self,$class; | |||||||||
| 48 | 5 | return $self; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 5 | ▶ | sub DESTROY { | ||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 5 | my $self=shift; | |||||||||
| 53 | 5 | $self->commit(); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 26 | ▶ | sub commit { | ||||||||
|
Subroutine
commit: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 57 | 26 | my $self=shift; | |||||||||
| 58 | |||||||||||
| 59 | 26 | my $str=""; | |||||||||
| 60 | 26 | my $delim=""; | |||||||||
| 61 | 26 | for my $var (keys %{$self->{"cfg"}}) { | |||||||||
| 62 | 154 | $var=~s/\n%/\n%%/g; | |||||||||
| 63 | 154 | my $val=$self->{"cfg"}->{$var}; | |||||||||
| 64 | 154 | $val=~s/\n%/\n%%/g; | |||||||||
| 65 | 154 | my $cfg="$var"."=".$val; | |||||||||
| 66 | 154 | $str.=$delim.$cfg; | |||||||||
| 67 | 154 | $delim="\n%"; | |||||||||
| 68 | } | ||||||||||
| 69 | 26 | ${$self->{"ref"}}=$str; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 11 | ▶ | sub set { | ||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 73 | 11 | my ($self,$var,$val)=@_; | |||||||||
| 74 | 11 | $self->{"cfg"}->{$var}=$val; | |||||||||
| 75 | 11 | $self->commit(); | |||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 39 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 79 | 39 | my ($self,$var)=@_; | |||||||||
| 80 | 39 | return $self->{"cfg"}->{$var}; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 10 | ▶ | sub del { | ||||||||
|
Subroutine
del: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 84 | 10 | my ($self,$var)=@_; | |||||||||
| 85 | 10 | delete $self->{"cfg"}->{$var}; | |||||||||
| 86 | 10 | $self->commit(); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 6 | ▶ | sub variables { | ||||||||
|
Subroutine
variables: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 90 | 6 | my $self=shift; | |||||||||
| 91 | 6 | return keys %{$self->{"cfg"}}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | 3 | 1; | |||||||||
| 95 | __END__ | ||||||||||