2

我刚刚找到了Data::Section,我对它很感兴趣。不幸的是,我根本无法package Letter::Resignation运行该页面上的示例。

那里有 Data::Section 的工作示例吗?

4

1 回答 1

2

好吧,在网上进行了一些不那么简单的搜索之后,我终于设法在网上找到了一个(我猜也是唯一的)我可以在这里阅读的示例:

最后向我解释了如何设置 Data::Section - 诀窍是它总是需要一个包名引用。最后,对于扩展部分中的变量,如何扩展文本字符串中的变量?- 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 }}

希望这对某人有所帮助 - 干杯!

于 2012-06-08T08:47:33.093 回答