Skip to content

Instantly share code, notes, and snippets.

@Skarsnik
Created November 13, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skarsnik/2db25f0b9ab52dc0635b to your computer and use it in GitHub Desktop.
Save Skarsnik/2db25f0b9ab52dc0635b to your computer and use it in GitHub Desktop.
use Config::Simple::Role;
class Config::Simple does Config::Simple::Role {
multi method new($filename) {
return self.bless(:filename($filename));
}
multi method new($filename, Str :$b) {
my $module = "Config::Simple::$b";
require ::($module);
return ::($module).new($filename);
}
multi method read($filename) {
my $text = slurp($filename);
my $this = self.bless(:filename($filename), :hash(EVAL($text)));
return $this;
}
method read($filename, Str :$b) {
my $module = "Config::Simple::$b";
require ::($module);
return ::($module).read($filename);
}
method write($filename = Any) {
$.filename = $filename if $filename.defined;
say $.filename;
my $fh = open $.filename, :w;
$fh.print(%!hash.perl);
$fh.close;
}
}
role Config::Simple::Role {
has %.hash is rw;
has $.filename is rw;
method AT-KEY($key) {
my $self = self;
Proxy.new(
FETCH => method ()
{
$self.hash{$key};
},
STORE => method ($val)
{
$self.set($key, $val);
}
);
}
method set($key, $val) {
%.hash{$key} = $val;
}
method new($filename){ ... };
method read($filename) { ... };
method write($filename = Any) { ... };
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment