#!/usr/bin/perl
use
YAML
qw(LoadFile Load)
;
sub
new {
my
(
$class
,
%options
) =
@_
;
my
$self
= {};
bless
$self
,
$class
;
$self
->{text} =
$options
{text}
if
exists
$options
{text};
$self
->{file} =
$options
{file}
if
exists
$options
{file};
if
(!
exists
$self
->{text} and
!
exists
$self
->{file}) {
die
"usage: "
, __PACKAGE__,
"->new(file => \$filename) or "
,
__PACKAGE__,
"->new(text => \$text)"
;
}
return
$self
;
}
sub
parse {
my
(
$self
) =
@_
;
my
$data
= {};
if
(
exists
$self
->{text}) {
$self
->{data} = Load(
$self
->{text});
}
my
@todo
= (
$self
->{data});
while
(
@todo
) {
my
$ref
=
shift
@todo
;
for
(
keys
%$ref
) {
if
(
ref
(
$ref
->{
$_
}) eq
"HASH"
) {
push
@todo
,
$ref
->{
$_
};
}
elsif
(
$_
eq
"name"
) {
$ref
->{value} =
$ref
->{
$_
};
delete
$ref
->{
$_
};
}
else
{
my
$tmp
=
$ref
->{
$_
};
$ref
->{
$_
} = {};
$ref
->{
$_
}->{value} =
$tmp
;
}
}
}
return
$self
->{data};
}
my
$p
= MyYAMLParser->new(
text
=>
<<EOT);
category:
Bar:
Twix: WARN, Screen, Screen2
appender:
Screen:
name: Log::Log4perl::Appender::Screen
layout: Log::Log4perl::Layout::SimpleLayout
Screen2:
name: Log::Log4perl::Appender::Screen
layout: Log::Log4perl::Layout::SimpleLayout
EOT
Log::Log4perl->init(
$p
);
my
$log
= Log::Log4perl->get_logger(
"Bar::Twix"
);
$log
->
warn
(
'foo'
);