perl6 'do(file)' equivalent

ぐ巨炮叔叔 提交于 2019-12-05 09:34:21

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

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.";
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!