导出/打印 Perl AppConfig 值

发布于 2024-11-19 18:20:07 字数 304 浏览 3 评论 0原文

我正在使用 Perl 和 PHP 解析单个配置文件,并且我想绝对确保它们得到完全相同的结果。因此,我想将解析的配置导出到另一个文件,或者(最好)只是打印它(按字母顺序按键排序)。有没有一些简单的方法可以做到这一点,而不会对 $config->varlist(".")$config->_dump() 结果进行一些丑陋的解析?它们都包含垃圾,例如 hash() 键、一些名为 1 的键以及 AppConfig 配置选项的值(例如 PEDATIC)。

I'm parsing a single configuration file with both Perl and PHP, and I want to make absolutely sure that they get exactly the same result. Therefore I'd like to either export the parsed configuration to another file or (preferably) just print it (sorted alphabetically by key). Is there some simple way to do this without some ugly parsing of the $config->varlist(".") or $config->_dump() results? These both contain junk like hash() keys, some key called 1 and the values of the AppConfig configuration options like PEDANTIC.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2024-11-26 18:20:07

最终得到以下适用于严格、警告和污点模式的代码:

my %config_state = (
    CASE => 1,
    PEDANTIC => 1);
my %config_defs = ('db_user=s' => {}, ...);
...
my $config = AppConfig->new(%config_state);
for my $key (keys %config_defs) {
    $config->define($key => $config_defs{ $key })
}
...
# Dump configuration to temporary file
open CONFIG, '>configuration.ini' or die $!;
my $varname;
for my $key (sort keys %config_defs) {
    $varname = substr $key, 0, -2;
    print CONFIG "$varname = ";
    print CONFIG $config->get($varname) if defined($config->get($varname));
    print CONFIG "\n";
}
close CONFIG or die $!;

Ended up with the following code which works with strict, warning and taint mode:

my %config_state = (
    CASE => 1,
    PEDANTIC => 1);
my %config_defs = ('db_user=s' => {}, ...);
...
my $config = AppConfig->new(%config_state);
for my $key (keys %config_defs) {
    $config->define($key => $config_defs{ $key })
}
...
# Dump configuration to temporary file
open CONFIG, '>configuration.ini' or die $!;
my $varname;
for my $key (sort keys %config_defs) {
    $varname = substr $key, 0, -2;
    print CONFIG "$varname = ";
    print CONFIG $config->get($varname) if defined($config->get($varname));
    print CONFIG "\n";
}
close CONFIG or die $!;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文