perl6 'do(file)' 等效 [英] perl6 'do(file)' equivalent

查看:37
本文介绍了perl6 'do(file)' 等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 perl5 中,我曾经为这样的配置文件做(文件)":

In perl5 I used to 'do (file)' for configuration files like this:

---script.pl start ---
our @conf = ();
do '/path/some_conf_file';
...
foreach $item (@conf) {
    $item->{rules} ...
...
---script.pl end ---

---/path/some_conf_file start ---
# arbitrary code to 'fill' @conf
@conf = (
{name => 'gateway',
    rules => [
        {verdict => 'allow', srcnet => 'gw', dstnet => 'lan2'}
    ]
},

{name => 'lan <-> lan2',
    rules => [
        {srcnet => 'lan', dstnet => 'lan2',
         verdict => 'allow', dstip => '192.168.5.0/24'}
    ]
},
);
---/path/some_conf_file end ---

Larry Wall 的Programming Perl"也提到了这个方法:

Also Larry Wall's "Programming Perl" also mentions this method:

但是do FILE对于阅读程序这样的东西还是很有用的配置文件.手动错误检查可以通过这种方式完成:

But do FILE is still useful for such things as reading program configuration files. Manual error checking can be done this way:

# read in config files: system first, then user 
for $file ("/usr/share/proggie/defaults.rc",
                "$ENV{HOME}/.someprogrc") {
         unless ($return = do $file) {
             warn "couldn't parse $file: $@" if $@;
             warn "couldn't do $file: $!"    unless defined $return;
             warn "couldn't run $file"       unless $return;
         } }

好处:

  • 不需要每次都编写自己的解析器 - perl 解析和为您创建数据结构;
  • 更快/更简单:原生 perl 数据没有从外部格式(如 YAML)转换的开销的结构/类型;
  • 不需要操纵@INC来加载来自某处的模块与作为 conf 文件的模块相比;
  • 减少额外费用代码与模块作为 conf 文件进行比较;
  • 配置文件"的语法"和perl一样强大;
  • 临时"格式;

缺点:

  • 没有隔离:我们可以从配置"中执行/销毁任何东西文件";

如何使用 perl6 获得相同的效果?
有没有办法在 perl6 中做得更好(没有缺点)并且不解析自己的语法、语法、模块包括?
诸如从文件中的文本表示中加载哈希或数组"之类的东西?

How do I get the same with perl6?
Is there way to do it better in perl6 (without Disadvantages) and without parsing own syntax, grammars, module including?
Something like "Load hashes or arrays from text representation from file"?

推荐答案

您可以使用 EVALFILE($file) (ref. http://doc.perl6.org/language/5to6-perlfunc#do).

You can use EVALFILE($file) (ref. http://doc.perl6.org/language/5to6-perlfunc#do).

正如你所指出的,使用 EVALFILE 有缺点,所以我不会在这个方向上添加任何东西:-)

As you pointed out, using EVALFILE has disadvantages, so I'm not going to add anything in that direction :-)

这是一个示例配置文件:

Here's a sample configuration file:

# Sample configuration (my.conf)
{
    colour  => "yellow",
    pid     => $*PID,
    homedir => %*ENV<HOME> ~ "/.myscript",
    data_source => {
        driver => "postgres",
        dbname => "test",
        user   => "test_user",
    }
}

这是一个使用它的示例脚本:

and here's a sample script using it:

use v6;

# Our configuration is in this file
my $config_file = "my.conf";
my %config := EVALFILE($config_file);

say "Hello, world!\n";

say "My homedir is %config<homedir>";
say "My favourite colour is %config<colour>";
say "My process ID is %config<pid>";
say "My database configuration is:";
say %config<data_source>;

if $*PID != %config<pid> {
    say "Strange. I'm not the same process that evaluated my configuration.";
}
else {
   say "BTW, I am still the same process after reading my own configuration.";
}

这篇关于perl6 'do(file)' 等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆