Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl6 'do(file)' equivalent

Tags:

raku

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 ---

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

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;
         } }

Benefits:

  • does not require write your own parser each time - perl parse and create data structures for you;
  • faster/simpler: native perl data structures/types without overheads for converting from external format (like YAML);
  • does not require manipulate @INC to load the module from somewhere compared to module as conf file;
  • less extra code compared to modules as conf file;
  • "syntax" of "configuration file" is powerful as perl itself;
  • "ad hoc" format;

Disadvantages:

  • no isolation: we can execute/destroy anything from "configuration file";

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"?

like image 915
gapsf Avatar asked Sep 25 '22 18:09

gapsf


1 Answers

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.";
}
like image 138
Cosimo Avatar answered Oct 11 '22 12:10

Cosimo