reading .ini file without using a module

A

alfonsobaldaserra

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
 
G

George Mpouras

Στις 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 !
 
A

alfonsobaldaserra

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top