Re: Reading a Properties file
by davorg (Chancellor) on Aug 16, 2006 at 15:26 UTC
|
I searched CPAN for "properties" and found Config::Properties. Is that useful? You might need to be a little clearer about what you mean by a "properties file".
--
< http://dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
|
Hello
properties file i mean to say basically a text file with that format...so that the script will be independent and if i change the target directory i can do it in text file and my script won't be touched it should work with that change and if i want to add say *.xlsi can add it in text file and no need of changing the script hope am clear
Thanks & Regards
Sridhar
| [reply] |
|
| [reply] |
|
Boy, do you ever read your own posts? If so, I wonder why you don't realize that </br> tags do absolutely nothing. Make that <br />, please.
| [reply] [d/l] [select] |
|
A reply falls below the community's threshold of quality. You may see it by logging in.
|
Re: Reading a Properties file
by Trix606 (Monk) on Aug 16, 2006 at 15:45 UTC
|
Also take a look at Config::Tiny it should do what you require. | [reply] |
|
| [reply] |
|
In that case use something more powerful like Config::Simple, it allows for multiple lines.
| [reply] |
Re: Reading a Properties file
by rblasch (Monk) on Aug 16, 2006 at 21:20 UTC
|
I once had a similar problem. The properties file was used by a Java program, in java.util.Properties format. I used Config::Properties, which worked nicely for me.
I also couldn't install modules on the machine, so I distributed the module with the script, by putting Properties.pm into a subdirectory lib/Config and told the script to use lib 'lib';. See lib. If your current directory isn't the directory that contains the script have a look at FindBin.
Hope that helps a bit.
| [reply] [d/l] [select] |
Re: Reading a Properties file
by mantra2006 (Hermit) on Aug 16, 2006 at 16:25 UTC
|
Hello Monks
I didn't permissons to download CPAN modules..so I came up with the following code suggest me if anything is wrong
$config = "C:\\Testing\\file.ini";
# check if file exists and can be read
-r $config || die "$config does not exists or is not readable";
open(IN, $config) || die "Cannot open $config for reading: $!";
sub readFile
{
my $key = "";
my $value = "";
# initialize rule storage
my $source_dir = "";
my $target_dir = "";
my @pattern;
while(<IN>) # read a line from line into $_
{
$line = $_;
if ( /^(\s*|\;.*|\#.*)$/i )
{
# empty line or comment
}
elsif ( ($key, $value) = /^\$([A-Z_]+)\=(.+)/ )
{
#significant line: $key=$value
$value = evalValue($value);
#$key = lc($key);
# look for file attributes
$source_dir=$value if( $key eq "SOURCE_DIR" );
$target_dir=$value if ( $key eq "TARGET_DIR" );
@pattern=$value if ( $key eq "PATTERN" );
}
}
}
sub evalValue
{
my $value = $_[0];
my $key = "";
my $result = "";
my $rest = "";
$_ = $value;
$rest = $value;
while( ($key) = /\$([A-Z_]\w*)/)
{
$key_val{$key} || die "Undefined key: $key $!";
$result = $result . $` . $key_val{$key};
$_ = $';
$rest = $';
}
$result = $result . $rest;
return $result;
}
This way I can read all the values and then process accordingly
Thanks again for suggestions
Sridhar | [reply] [d/l] |
|
You can't download a module, but you can write your own code? I'm having a hard time with this concept. At the very least you can download the module and include its source code within your program. Or you could install the module in your "disk area" and use it from there. If anyone asks, we'll tell them that you wrote it. :-)
| [reply] |
A reply falls below the community's threshold of quality. You may see it by logging in. |