On 2020-05-13 22:27, Bruce Gray wrote:
On May 13, 2020, at 9:37 PM, ToddAndMargo via perl6-users
<perl6-users@perl.org> wrote:
Hi All,
Do we have anything like Bash's "." statement where
we can read in a bunch of values from a .cfg file?
(I think it is called "include", but I am not sure.)
. /etc/sysconfig/network-scripts/ifcfg-br0
which populates these (and other) variables
DEVICE=br0
TYPE=Bridge
ONBOOT=yes
USERCTL=yes
DELAY=0
NM_CONTROLLED=yes
BOOTPROTO=none
PREFIX=24
...
Many thanks,
-T
Hi Todd,
FYI, the `.` Bash command is also called `source`, which is easier to search on
the Web, and clearer in email:
https://ss64.com/bash/source.html
The closest equivalent in Raku is:
https://docs.raku.org/routine/EVALFILE
, which could be used for config data like so:
$ cat a.dat
$foo = "bar";
$baz = "quxx";
$ perl6 -e 'our ($foo, $baz); EVALFILE "a.dat"; .say for $foo, $baz;'
bar
quxx
, but please do not use it for this purpose.
EVALFILE is in all-caps to show that it might be dangerous and not for general
use; it is “grep-able evil”, and could eval any valid Raku code, even evil
things like `run “rm -rf /“`.
IMHO, Bash's `source`-style of loading variables pollutes the main namespace
and causes hard-to-debug “action at a distance”.
In Raku (or any other dynamic language), the use of some kind of Config module
is safer and cleaner:
https://modules.raku.org/t/CONFIG
https://github.com/raku-community-modules/perl6-Config-JSON
https://github.com/Skarsnik/perl6-config-simple
https://metacpan.org/pod/Config::Tiny
For example:
$ cat config.json
{
"baz": "quxx",
"foo": "bar”
}
$ perl6 -e 'use Config::JSON; my %c; %c{$_} = jconf($_) for <foo baz>; say %c{$_} for
<foo baz>;'
bar
quxx
$ cat b.dat
foo = bar
baz = quxx
$ perl6 -e 'use Config::Tiny:from<Perl5>; my $conf = Config::Tiny.read("b.dat"); .say for
$conf<_><foo baz>'
bar
quxx
—
Hope this helps,
Bruce Gray (Util of PerlMonks)
Hi Bruce,
I looked at the first two links above. Neither showed
the format of the data being read. But you did. Is
there some reason why the two links did not show the format?
-T