Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl script accessing a properties file

by monk monk (Initiate)
on May 21, 2007 at 10:23 UTC ( #616523=perlquestion: print w/ replies, xml ) Need Help??
monk monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.. This is my 1st post here.. Can any one post me with a sample perl script that reads the properties file in a specified path. The properties file "abc.properties" can have values like name=ant password=gilly level=5 Thanks !!

Comment on Perl script accessing a properties file
Re: Perl script accessing a properties file
by andreas1234567 (Vicar) on May 21, 2007 at 10:46 UTC
    Configuration files has been discussed previously, for instance here, and here.

    Super Search is a good way to find answers on perlmonks.

    Andreas
Re: Perl script accessing a properties file
by jettero (Monsignor) on May 21, 2007 at 10:48 UTC

    See How do I post a question effectively?

    Generally, you'll find that people here will go out of their way to avoid writing your program for you — they might even offer to present their "rate card." Normally you would present code that you wrote that doesn't work and ask about it. Or ask how to approach a problem. Things like that.

    However...

    my %o; open my $in, "abc.properties" or die $!; while(<$in>) { $o{$1}=$2 while m/(\S+)=(\S+)/g; } close $in;

    -Paul

      thanks paul it was a gr8 help indeed
Re: Perl script accessing a properties file
by salva (Abbot) on May 21, 2007 at 11:40 UTC
Re: Perl script accessing a properties file
by leocharre (Priest) on May 21, 2007 at 12:09 UTC
    If you want to read things inside a file, as if the text inside it is config data- you want to use something like YAML to code with. If what you want is to be able to associate metadata (like the filename) onto a file, any kind of file- you can use File::Attributes::Extended or File::PathInfo::Ext
Reaped: Re: Perl script accessing a properties file
by NodeReaper (Curate) on Jul 21, 2011 at 13:09 UTC
Reaped: Re: Perl script accessing a properties file
by NodeReaper (Curate) on Jul 21, 2011 at 13:09 UTC
Re: Perl script accessing a properties file
by rajeshatbuzz (Novice) on Jul 21, 2011 at 13:11 UTC
    I just added following code to print value of each value...but it was blank.. Any reason why??
    my %o; open my $in, "file.properties" or die $!; while(<$in>) { $o{$1}=$2 while m/(\S+)=(\S+)/g; print "$1 and $2"; } close $in;

      So, what does file.properties look like? If the file exists but it empty, your script will not produce any output but will not fail either. If the file contains at least one line, your program will output at least one line containing the word "and". Note that the following line implies that you expect more than one key=value pair per line:

      $o{$1}=$2 while m/(\S+)=(\S+)/g;

      Is that really what your configuration file looks like?

      Maybe your input file (file.properties) is not what you think it is. Show use a few lines of it.

      This works for me:

      use warnings; use strict; my %o; while (<DATA>) { $o{$1}=$2 while m/(\S+)=(\S+)/g; print "$1 and $2\n"; } __DATA__ name=ant password=gilly level=5
      Prints:
      name and ant password and gilly level and 5
      See also:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-12-29 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (185 votes), past polls