On 2020-05-14 08:22, Peter Pentchev wrote:
On Thu, May 14, 2020 at 04:39:18AM -0700, ToddAndMargo via perl6-users wrote:
On 2020-05-13 22:27, Bruce Gray wrote:


On May 13, 2020, at 9:37 PM, ToddAndMargo via perl6-users 
<perl6-us...@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?

Well, they do both say they read .ini-style files. I think that they
will both be able to read simple key=value files like the network
definition sysconfig ones on RedHat-style systems that you seem to want.
Keep in mind that the shell probably interprets a bit more, so some
configuration-reading modules may e.g. return the quotes around the
value or something like that; take them for a spin and see.
Also, it's almost certain that these modules will not be able to help if
the files that you read make use of the fact that the shell performs
variable expansion: they will not be able to expand other variables in
lines like:

KEYFILE="/etc/keys/$HOSTNAME.key"

or something like that.

If you come across files like that, you may have to write your own
parser.

For some general information on ini-like files, see
https://en.wikipedia.org/wiki/INI_file

G'luck,
Peter


Hi Peter,

That was extremely helpful!  I never realized INI
files were standardized.

I will be playing with them shortly.  I really want
to see how CONFIG handles section titles.

Thank you!

-T

Reply via email to