Follow up.

Special thanks to Peter and David!

I wrote a sample program to explain things in
my personal documentation:

-T


Raku: reading INI files:


Example:

~~~~~~~~~ini.test.pl6.ini ~~~~~~~~~~~
# Raku: Config::INI test INI
# edit at your own risk

IHave=NoSection

[Backup paramters]
target=B:\myDocsBackp\backup1
partition=BACKUP

[eMail]
smtp=smtp.bozo.com
address=b...@theclown.com
port=587
~~~~~~~~~/ini.test.pl6.ini ~~~~~~~~~~


~~~~~~~~~~~ini.test.pl6 ~~~~~~~~~~~~~
#! /usr/bin/env raku

=begin introduction

    # zef install Config
    # zef install Config::INI



https://github.com/tadzik/perl6-Config-INI/blob/master/lib/Config/INI.pm
    line 45 starts documtation

    use Config::INI;
    my %IniHash = Config::INI::parse_file('config.ini');

    # Config::INI::parse return a hash of a hash
    %IniHash = Config::INI::parse($file_contents);

    # items before a [section] tag are given a [section] key value of '_'
    print %hash<_><root_property_key> ~ "\n";

    # Otherwise the key is the [section] tag
    print  %hash<section><in_section_key> ~ "\n";

       IHave=NoSection
       [eMail]
       address=b...@theclown.com

    print "[_] IHave = " ~ %IniHash<_><IHave> ~ "\n";
    print "[eMail] Address = " ~ %IniHash<eMail><address> ~ "\n";

        [_] IHave = NoSection
        [eMail] Address = b...@theclown.com

=end introduction


use Config;
use Config::INI;

my Str $IniFile = slurp "ini.test.pl6.ini";
my %IniHash = Config::INI::parse($IniFile);  # %IniHash is a hash of a hash

print "The following INI file was read:\n";
for $IniFile.lines -> $Line { print "    $Line\n"; }
print "\n";

print "The following sections where located:\n";
for %IniHash.kv -> $key, $value  { print "    [" ~ $key ~ "]\n"; }
print "\n";

print "The following is a list of INI variables located in each section:\n";
for %IniHash.kv -> $section, $value  {
   print "    [" ~ $section ~ "]\n";
   for $value.kv -> $SectionKey, $SectionValue  {
      print "       $SectionKey=$SectionValue\n";
   }
}
print "\n";

print "Direct addressing of values:\n";
print "    [_] IHave = " ~ %IniHash<_><IHave> ~ "\n";
print "    [eMail] Address = " ~ %IniHash<eMail><address> ~ "\n";
print "\n";
~~~~~~~~~~~/ini.test.pl6 ~~~~~~~~~~~~


$ ini.test.pl6
The following INI file was read:
    # Raku: Config::INI test INI
    # edit at your own risk

    IHave=NoSection

    [Backup paramters]
    target=B:\myDocsBackp\backup1
    partition=BACKUP

    [eMail]
    smtp=smtp.bozo.com
    address=b...@theclown.com
    port=587


The following sections where located:
    [_]
    [eMail]
    [Backup paramters]

The following is a list of INI variables located in each section:
    [_]
       IHave=NoSection
    [eMail]
       port=587
       address=b...@theclown.com
       smtp=smtp.bozo.com
    [Backup paramters]
       target=B:\myDocsBackp\backup1
       partition=BACKUP

Direct addressing of values:
    [_] IHave = NoSection
    [eMail] Address = b...@theclown.com

Reply via email to