Home > Archive > PERL Miscellaneous > March 2006 > Using OOP to load a config file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Using OOP to load a config file
|
|
| Prab_kar@hotmail.com 2006-03-27, 7:00 pm |
| Hi all,
I'm fairly new to OOP in Perl and trying my hand to load a config file
(in a 'key=value' format)using OOP. I've got stuck at a point and
wonder if anyone of you could give me some pointers.
Config file: Foo.conf
Perl module: CfgLoader.pm
Perl script: useOOP.pl
For a given config file, Foo.conf with content along the lines,
Key1=Value1, I'd like to access the keys and values from my Perl
script, useOOP.pl, as, $obj->value("key1") ; which would give me
"value1".
In the following .pl/.pm below, I was able to load the config file and
split the key/values, but dont know how to transfer that 'hash' back to
the main program, useOOP.pl.
I'll have to define another method/function, 'value', but dont know how
to transfer the hash from the 'loadCfg' method to the new method or to
return values from the new method to the calling script. Could any of
you give some pointers, please? Thanks for your time,
------------------useOOP.pl:-----------------------------
#!/usr/local/bin/perl
use strict ;
use warnings ;
use CfgLoader ;
my $obj = new CfgLoader() ;
$obj->loadCfg("Foo.conf") ;
--------------------------------------------------------------
------------------CfgLoader.pm-----------------------------
package CfgLoader ;
sub new ()
{
$class = shift ;
$self = {} ;
bless ($self,$class) ;
}
sub loadCfg()
{
my ($self,$file) = @_ ;
print "Loading configuration: $file\n" ;
open(IN,"$file") or die "Cant open $file: $!\n" ;
while(<IN> )
{
chomp ;
next if ( /^(#|$)/ ) ;
($key,$value) = split(/\=/,$_) ;
print "Key: $key Value: $value\n" ;
$hash{$key} = $value ;
}
close(IN) or die "Cant close file: $!\n" ;
}
# Required to retun 1
1 ;
-------------------------------------------------------------------------
Thanks,
Prabh
| |
| Todd W 2006-03-27, 7:00 pm |
|
<Prab_kar@hotmail.com> wrote in message
news:1143494376.170932.292460@z34g2000cwc.googlegroups.com...
> Hi all,
> I'm fairly new to OOP in Perl and trying my hand to load a config file
> (in a 'key=value' format)using OOP. I've got stuck at a point and
> wonder if anyone of you could give me some pointers.
>
<snip custom ini parser>
If you are doing it for an exercise thats fine, otherwise you should use a
CPAN module. That approach is much more likely to be more featureful and
have fewer bugs than a roll-your-own.
For this particular problem, I use Config::INI::Simple.
http://search.cpan.org/~kirsle/Conf...g/INI/Simple.pm
Todd W.
| |
| Prab_kar@hotmail.com 2006-03-27, 9:59 pm |
|
Todd W wrote:
> <Prab_kar@hotmail.com> wrote in message
> news:1143494376.170932.292460@z34g2000cwc.googlegroups.com...
>
>
> <snip custom ini parser>
>
> If you are doing it for an exercise thats fine, otherwise you should use a
> CPAN module. That approach is much more likely to be more featureful and
> have fewer bugs than a roll-your-own.
>
> For this particular problem, I use Config::INI::Simple.
>
> http://search.cpan.org/~kirsle/Conf...g/INI/Simple.pm
>
> Todd W.
Thanks for the response, Todd.
1. Yes, I indeed am using this as an exercise.
2. I'm supposed to use vendor-built version of Perl, which I dont think
has Config::INI::Simple (I'm using IBM Rational Clearcase's CCPerl.)
Thanks,
Prabh
|
|
|
|
|