我刚刚找到Data::Section,我对此感兴趣。不幸的是,我无法在该页面上运行package Letter::Resignation
示例。
那里有Data :: Section的工作示例吗?
答案 0 :(得分:2)
好吧,经过网上一些不那么重要的搜索,我终于找到了网上我可以阅读的那个(也是唯一的,我猜)的例子:
最后向我解释了如何设置Data :: Section - 技巧是它总是需要一个包名引用。最后,为了扩展部分中的变量,How can I expand variables in text strings? - perlmonks.org非常有用。
如果您不想使用包名称,那么这是一个MWE:
use strict;
use warnings;
use utf8;
use charnames ':full';
# sudo perl -MCPAN -e shell
# install Data::Section
use Data::Section -setup;
my $name = "testing \t escapes \n variable";
my $sections = main::->merged_section_data;
for my $filename (keys %$sections) {
printf "== %s ==\n%s\n", $filename, main::->section_data($filename);
}
my $template = main::->section_data ("test_file");
# expands variables in the template as well!!
$$template =~ s/(\$\w+)/$1/eeg;
die if $@; # needed on /ee, not /e
print $$template;
__DATA__
__[ test_file ]__
\t testing \r escapes \n data::section \t
Here
--
{{ $name }}
__END__
如果您使用包名称,只是略有不同:
package aPerlTest;
use strict;
use warnings;
use utf8;
use charnames ':full';
# sudo perl -MCPAN -e shell
# install Data::Section
use Data::Section -setup;
my $name = "testing \t escapes \n variable";
my $sections = aPerlTest::->merged_section_data;
for my $filename (keys %$sections) {
printf "== %s ==\n%s\n", $filename, aPerlTest::->section_data($filename);
}
my $template = aPerlTest::->section_data ("test_file");
# expands variables in the template as well!!
$$template =~ s/(\$\w+)/$1/eeg;
die if $@; # needed on /ee, not /e
print $$template;
__DATA__
__[ test_file ]__
\t testing \r escapes \n data::section \t
Here
--
{{ $name }}
__END__
在这两种情况下,终端输出为:
$ perl aPerlTest.pl
== test_file ==
SCALAR(0x9f079d0)
\t testing \r escapes \n data::section \t
Here
--
{{ testing escapes
variable }}
希望这有助于某人 - 干杯!