Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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 "" can have values like name=ant password=gilly level=5 Thanks !!
  • Comment on Perl script accessing a properties file

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

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.


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


      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
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, "" or die $!; while(<$in>) { $o{$1}=$2 while m/(\S+)=(\S+)/g; print "$1 and $2"; } close $in;

      So, what does 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 ( 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
      name and ant password and gilly level and 5
      See also:

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://616523]
Approved by varian
[Corion]: ambrus: Yeah, I read that, but it's somewhat vague as in what I really have to implement, and where/how my "other" mainloop should/needs to call AnyEvent
[Corion]: (or maybe I just work better from existing code that I munge until it works and I understand it rather than a short abstract text like "implement everything that's needed" ;) )
[ambrus]: Corion: I think in this case you can get away with only a stub for idle, one that always dies when you create it, because AnyEvent::HTTP doesn't use it, not even indirectly through AnyEvent::Handle or AnyEvent::Socket or AnyEvent::DNS.
[Corion]: The "and I understand it" part is optional.
[Corion]: ambrus: Yes but I also need to implement the file / IO watcher, because Prima has that (in Prima::File), and I need to supply the appropriate thing to make push_write etc. work with Prima
[ambrus]: Corion: yes, you need to implement the io watcher, which should be simple because Prima::File is basically that, and the timer watcher form Prima::Timer
[Corion]: ... or so I think. As I said, I'm somewhat vague on how to make AnyEvent cooperate with a callback-driven IO event loop that gives me callbacks when data is available or can be written
[ambrus]: what push_write thing? I don't think you need that. that's implemented generically by AnyEvent::Handle
[Corion]: ambrus: Yeah, that's what I think as well. But you give me an idea, maybe I should start with implementing the timer, as that should be far simpler and with fewer edge-cases/nasty interaction than the file watcher
[ambrus]: You only provide the watcher part that tells when the handle is readable or writable, not the actual writing and reading.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-08 12:15 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (141 votes). Check out past polls.