當前使用perl解析由多個部分組成的配置文件,該文件又可以由多個定義組成。我的實際文件有一個完全不同的話題,但我的解析器將解析這個例子中,太:將值存儲在散列表中
SECTION Foo
HP 200
ATT 50 Slap
ATT 100 Kick
DESC This is Foo. What is love?
SECTION Bar
HP 2
ATT 1 Mumble
DESC This is Bar. Baby don't hurt me!
現在,我的解析器主要採用三個變量來存儲數據:
my %sections;
my $parsedName;
my %parsedVars;
在閱讀SECTION Bar
,它已填寫如下:
%sections =(); # empty
$parsedName = "Foo";
%parsedVars = (
"HP" => "200",
"ATT" => ("50 Slap","100 Kick"),
"DESC" => "This is Foo. What is love?",
);
我想你明白了。現在,%parsedVars
字段的內容進行驗證,如果成功的話,整個事情被存儲到%sections
,這是我的代碼使用的有:
use Storable qw(dclone);
# (...)
# Clone the Variables
$sections{$parsedName} = dclone (\%parsedVars);
# Prepare for next section
$parsedName = getSectionName $currentLine;
undef %parsedVars;
這是傷害的一部分,因爲我不由於嚴格限制的運行時環境,我真的不想深入複製整個%parsedVars
,並且我也不允許包含除strict
以外的任何內容。
我覺得我應該從它的名字中分離哈希,並將它附加到$sections{$parsedName}
,但是我無法將我的頭圍繞如何完成。
# These hiccups aside,
# my parser works fine, which is nice.
# One completeness wide:
# Result looks like this, no surprise!
%sections = (
"Foo" => (
"HP" => "200",
"ATT" => ("50 Slap","100 Kick"),
"DESC" => "This is Foo. What is love?",
),
"Bar" => (
"HP" => "2",
"ATT" => "1 Mumble",
"DESC" => "This is Bar. Baby don't hurt me!",
),
);