在Perl中用YAML来实现配置文件
文章目录
通常,大家都习惯于用XML写配置文件或是描述一些简单的数据结构。其实,在Perl中,用YAML更好。有关YAML的详细说明,请参看这里:http://www.yaml.org/spec/
YAML是一个很适合人类阅读的标记语言,可以被大多数的现代语言用来作数据结构。
Perl的YAML.pm模块可以直接上CPAN下载,官方网页是:
http://search.cpan.org/~ingy/YAML-0.66/lib/YAML.pm
基本上读一读下面这段英文,就明白YAML怎么用了。
use YAML;
# Load a YAML stream of 3 YAML documents into Perl data structures.
my ($hashref, $arrayref, $string) = Load(<<'...');
---
name: ingy
age: old
weight: heavy
# I should comment that I also like pink, but don't tell anybody.
favorite colors:
- red
- green
- blue
---
- Clark Evans
- Oren Ben-Kiki
- Ingy döt Net
--- >
You probably think YAML stands for "Yet Another Markup Language". It
ain't! YAML is really a data serialization language. But if you want
to think of it as a markup, that's OK with me. A lot of people try
to use XML as a serialization format.
"YAML" is catchy and fun to say. Try it. "YAML, YAML, YAML!!!"
...
# Dump the Perl data structures back into YAML.
print Dump($string, $arrayref, $hashref);
# YAML::Dump is used the same way you'd use Data::Dumper::Dumper
use Data::Dumper;
print Dumper($string, $arrayref, $hashref);</pre>
文章作者 cookwhy
上次更新 2008-08-07