Created
November 13, 2015 21:25
-
-
Save Skarsnik/2db25f0b9ab52dc0635b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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