Config::Backend::String - String backend for Config::Frontend.


Config-Frontend documentation Contained in the Config-Frontend distribution.

Index


Code Index:

NAME

Top

Config::Backend::String - String backend for Config::Frontend.

Synopsys

Top

 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;

ABSTRACT

Top

Config::Backend::String is a backend module for Config::Frontend.

Description

Top

new(\$strref) --> Config::Backend::String

If called with a reference to a string, will instantiate a Config::Backend::String object. Otherwise will attempt to die.

set(var,val) --> void

This 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) --> string

Returns undef, if var does not exist. Returns the value of var (string), otherwise.

del(var) --> void

Delets var from the string.

variables() --> list

Will return a list of all variables in Conf::String.

SEE ALSO

Top

Config::Backend::String, Config::Backend::SQL, Config::Backend::File, Config::Frontend.

AUTHOR

Top

Hans Oesterholt-Dijkema, <oesterhol@cpan.org>

COPYRIGHT AND LICENSE

Top


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__