Skip to content

Instantly share code, notes, and snippets.

@bessarabov
Created November 6, 2013 07:20
Show Gist options
  • Save bessarabov/7332229 to your computer and use it in GitHub Desktop.
Save bessarabov/7332229 to your computer and use it in GitHub Desktop.
Исходный код Perl задачки http://perltrap.com/ru/konfiguracionnye-faily/
---
parallel-level: 4
timeout: 50
verbose: 0
port: 10018
locale: UTF-8
use strict;
use warnings;
package Config;
use YAML;
sub load
{
my ($class, %param) = @_;
my $conf;
if ( $param{file} =~ /\.yaml$/ ){
print $param{file}."\n";
$conf->{data} = YAML::LoadFile( $param{file} );
$conf->{format} = "yaml";
$conf->{file} = $param{file};
} else {
die "unsupported format of config, stop";
}
return bless $conf;
}
sub write
{
my ($self, %param) = @_;
YAML::DumpFile($param{file}, $self->{data});
return;
}
sub get
{
my ($self, $key) = @_;
return $self->{data}->{$key};
}
sub set
{
my ($self, $key, $value) = @_;
$self->{data}->{$key} = $value;
return;
}
1;
#!/usr/bin/perl
use strict;
use warnings;
use Config;
my $conf = Config->load(file => "conf.yaml");
my $parallel_level = $conf->get("parallel-level");
$conf->set("parallel-level", $parallel_level * 2);
$conf->write(file => "conf2.yaml");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment