reading .ini file without using a module

Discussion in 'Perl Misc' started by alfonsobaldaserra, Mar 16, 2011.

  1. hello group,

    i understand that there are modules to parse and read .ini files but
    just to understand data structures better i was playing with this.
    the following code is from perl developers dictionary:

    use strict;
    use warnings;
    use Data::Dumper;

    open my $fh, '<', "my.ini" or die "$!\n";
    my $ini = {};

    {
    local $/ = "";
    while (<$fh>) {
    next unless ( s/\[([^]]+)\]// );
    my $header = $1;
    $ini->{$header} = {};
    while (m/(\w+)=(.*)/g) {
    $ini->{$header}->{$1} = $2;
    }
    }
    }
    close $fh;

    print Dumper $ini;


    __DATA__
    [Build]
    BuildID=20110209115208
    Milestone=2.0b11
    SourceStamp=f9d66f4d17bf
    SourceRepository=http://hg.mozilla.org/mozilla-central
    ; This file is in the UTF-8 encoding

    [Strings]
    Title=SeaMonkey Update
    Info=SeaMonkey is installing your updates and will start in a few
    moments


    i could not figure a way to do something like

    print $ini->{Build}->{Milestone}
    # 2.0b11

    print $ini->{Strings}->{Title}
    # SeaMonkey Update

    could somebody lend me their hand to figure how to make this work?

    thanks
    alfonsobaldaserra, Mar 16, 2011
    #1
    1. Advertising

  2. Στις 16/3/2011 10:03 μμ, ο/η alfonsobaldaserra έγÏαψε:
    > use strict;
    > use warnings;
    > use Data::Dumper;
    >
    > open my $fh, '<', "my.ini" or die "$!\n";
    > my $ini = {};
    >
    > {
    > local $/ = "";
    > while (<$fh>) {
    > next unless ( s/\[([^]]+)\]// );
    > my $header = $1;
    > $ini->{$header} = {};
    > while (m/(\w+)=(.*)/g) {
    > $ini->{$header}->{$1} = $2;
    > }
    > }
    > }
    > close $fh;
    >
    > print Dumper $ini;
    >
    >
    > __DATA__
    > [Build]
    > BuildID=20110209115208
    > Milestone=2.0b11
    > SourceStamp=f9d66f4d17bf
    > SourceRepository=http://hg.mozilla.org/mozilla-central
    > ; This file is in the UTF-8 encoding
    >
    > [Strings]
    > Title=SeaMonkey Update
    > Info=SeaMonkey is installing your updates and will start in a few
    > moments
    >
    >
    > i could not figure a way to do something like
    >
    > print $ini->{Build}->{Milestone}
    > # 2.0b11
    >
    > print $ini->{Strings}->{Title}
    > # SeaMonkey Update
    >
    > could somebody lend me their hand to figure how to make this work?
    >
    > thanks


    try to create the my.ini with the same data and give a shot !
    George Mpouras, Mar 16, 2011
    #2
    1. Advertising

  3. > try to create the my.ini with the same data and give a shot !

    it was a very serious mistake. i am very sorry and thank you very
    much for correcting me.
    alfonsobaldaserra, Mar 17, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    0
    Views:
    422
    Michael
    Feb 6, 2004
  2. Vesa Leppanen

    Reading config.ini in PythonWin.

    Vesa Leppanen, Sep 4, 2006, in forum: Python
    Replies:
    3
    Views:
    375
    Gabriel Genellina
    Sep 12, 2006
  3. Replies:
    2
    Views:
    351
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Mar 13, 2007
  4. Jens Thoms Toerring

    Re: Reading .ini files in ANSI C

    Jens Thoms Toerring, Jun 18, 2008, in forum: C Programming
    Replies:
    2
    Views:
    3,773
    marika
    Jun 22, 2008
  5. Kishore Rp

    reading from a .ini file

    Kishore Rp, Jul 25, 2008, in forum: Ruby
    Replies:
    6
    Views:
    108
    Kishore Rp
    Jul 25, 2008
Loading...

Share This Page