in reply to Perl script accessing a properties file

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;

Replies are listed 'Best First'.
Re^2: Perl script accessing a properties file
by Corion (Patriarch) on Jul 21, 2011 at 13:16 UTC

    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?

Re^2: Perl script accessing a properties file
by toolic (Bishop) on Jul 21, 2011 at 13:19 UTC
    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: