P
Prab_kar
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 ;
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 ;