| Config-Frontend documentation | Contained in the Config-Frontend distribution. |
Config::Backend::String - String backend for Config::Frontend.
use Config::Frontend;
use Config::Backend::String;
open my $in,"<conf.cfg";
my $string=<$in>;
close $in;
my $cfg=new Config::Frontend(new Config::Backend::String(\$string))
print $cfg->get("config item 1");
$cfg->set("config item 1","Hi There!");
open my $out,">conf.cfg";
print $out $string;
close $out;
Config::Backend::String is a backend module for Config::Frontend.
new(\$strref) --> Config::Backend::StringIf called with a reference to a string, will instantiate a Config::Backend::String
object. Otherwise will attempt to die.
set(var,val) --> voidThis method will set variable var to value <val>. All set methods will
immediately reset the value of the string that the object references to.
So, all changes through 'set' will be visibile imediately to the
program environment.
get(var) --> stringReturns undef, if var does not exist.
Returns the value of var (string), otherwise.
del(var) --> voidDelets var from the string.
variables() --> listWill return a list of all variables in Conf::String.
Hans Oesterholt-Dijkema, <oesterhol@cpan.org>
Copyright 2004 by Hans Oesterholt-Dijkema
This library is free software; you can redistribute it and/or modify it under LGPL.
| Config-Frontend documentation | Contained in the Config-Frontend distribution. |
package Config::Backend::String; use 5.006; use strict; sub new { my $class=shift; my $strref=shift; my $self; if (ref($strref) ne "SCALAR") { die "Conf::String must be initialized with a reference to a string"; } $self->{"ref"}=$strref; my $str=${$strref}; my @cfgs; my $k=0; my $i=0; my $N=length $str; while ($i<$N) { if (substr($str,$i,2) eq "\n%") { if (substr($str,$i,3) ne "\n%%") { my $cfg=substr($str,$k,($i-$k)); $cfg=~s/\n%%/\n%/g; $k=$i+2; push @cfgs,$cfg; } else { $i+=1; } } $i+=1; } if ($str ne "") { my $cfg=substr($str,$k,$N); $cfg=~s/\n%%/\n%/g; push @cfgs,$cfg; } for my $cfg (@cfgs) { my ($var,$val) = split /=/,$cfg,2; $self->{"cfg"}->{$var}=$val; } bless $self,$class; return $self; } sub DESTROY { my $self=shift; $self->commit(); } sub commit { my $self=shift; my $str=""; my $delim=""; for my $var (keys %{$self->{"cfg"}}) { $var=~s/\n%/\n%%/g; my $val=$self->{"cfg"}->{$var}; $val=~s/\n%/\n%%/g; my $cfg="$var"."=".$val; $str.=$delim.$cfg; $delim="\n%"; } ${$self->{"ref"}}=$str; } sub set { my ($self,$var,$val)=@_; $self->{"cfg"}->{$var}=$val; $self->commit(); } sub get { my ($self,$var)=@_; return $self->{"cfg"}->{$var}; } sub del { my ($self,$var)=@_; delete $self->{"cfg"}->{$var}; $self->commit(); } sub variables { my $self=shift; return keys %{$self->{"cfg"}}; } 1; __END__