Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reading a Properties file

by mantra2006 (Hermit)
on Aug 16, 2006 at 15:22 UTC ( [id://567712]=perlquestion: print w/replies, xml ) Need Help??

mantra2006 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks


What is the best way to read a properties file using perl
program. suppose my properties file contains the following
source_dir="C:\\Testing\\Source"
target_dir="C:\\Testing\\Target"
pattern="*.txt *.dat *.doc"

pattern is an array..perl script should read this properties file
and get the details such as source_dir
into a string target_dir into a string
and pattern into an array
so that for each pattern
files will be checked and processed


Note: 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 *.xls
i can add it in text file and no need of changing the script
hope am clear
Thanks in advance for suggestions and sample code


Sridhar

Replies are listed 'Best First'.
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

      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 *.xls
      i can add it in text file and no need of changing the script
      hope am clear


      Thanks & Regards
      Sridhar

        In that case, your best approach is probably to pick one of the existing config file parsing modules and adjust your config file to match that syntax.

        --
        <http://dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

        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.


        holli, /regexed monk/
          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.
      I also have good experiences with Config::Tiny.

      The only problem is that I can't get it to set a property to multiple lines of text without patching the code. Am I doing something wrong?

        In that case use something more powerful like Config::Simple, it allows for multiple lines.
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.

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

      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. :-)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://567712]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-19 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found