我在它與下面的語句的文件:Perl:do和eval會導致不同的答案?
{
%{do '/tmp/personcontact.pl'},
%{do '/tmp/address.pl'}
}
現在,臨時文件如下: Personcontact.pl:
{
'firstname' => {
'__type' => 'String'
},
'lastname' => {
'__type' => 'String'
}
}
Address.pl:
{
'address' => {
'street' => {
'__type' => 'String'
},
'unit' => {
'__type' => 'String',
},
'suburb' => {
'__type' => 'String'
},
'__type' => 'HASH'
}
}
現在,當我這樣做:
open(SCHEMAFILE, "<", $schema) or return undef;
my $schemafile;
while(my $line = <SCHEMAFILE>) { $schemafile .= $line;}
my $tempref = eval $schemafile;
print Dumper $tempref;
結果是$VAR1 = '1/8'
當我做:
print Dumper do "/tmp/schemawithinschema.pl";
結果是
$VAR1 = 'firstname';
$VAR2 = {
'__type' => 'String'
};
$VAR3 = 'address';
$VAR4 = {
'suburb' => {
'__type' => 'String'
},
'unit' => {
'__type' => 'String'
},
'street' => {
'__type' => 'String'
},
'__type' => 'ARRAY'
};
$VAR5 = 'lastname';
$VAR6 = {
'__type' => 'String'
};
有什麼不對嗎?謝謝!
Hooooooooly牛。手動迭代(全局!)文件句柄SCHEMAFILE然後對其進行評估的部分是我上次告訴你的那個部分,這正是「做」的工作。你也是......只是......是的。 ** cry **把這些數據放入一個JSON或YAML文件中,相比之下,它的維護要容易900倍。 – 2011-01-24 08:23:38
同意。只有基於可存儲的配置在維護方面更糟糕。 (我甚至寫了一個腳本,做了一個Storable-> Dumper-> vim-> Dumper-> Storable轉換。) – Dallaylaen 2011-01-24 08:34:02