Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Config::Simple with vars method

by davidj (Priest)
on Jul 06, 2004 at 04:55 UTC ( [id://372021]=note: print w/replies, xml ) Need Help??


in reply to Config::Simple with vars method

First, Data::Dumper reveals why you are not getting anything returned.

#!/usr/bin/perl -w use Config::Simple; use strict; use Data::Dumper; my $cfg; my %config; $cfg = new Config::Simple('test.cfg'); open(FH,"test.cfg"); printf("'%s' syntax\n", $cfg->guess_syntax(\*FH)); close FH; %config = $cfg->vars(); print Dumper(\%config); exit; D:\PerlProjects\tests>c.pl
output:
'ini' syntax $VAR1 = { 'default.NET' => 'X' };
Now the reason for it (from a cursory reading of the Config::Simple manpage):
Config::Simple assumes config information is formatted in a block system, such as:
[block1] key=value
Since your config file does not have any block delimiters (indicated by brackets), the values are assigned to the 'default' block.
changing your config file to the following:
[info] NET=X
generates the following output:
D:\PerlProjects\tests>c.pl 'ini' syntax $VAR1 = { 'info.NET' => 'X' };
I would suggest you perldoc Config::Simple to learn more about the module.

hope this helps,
davidj

Replies are listed 'Best First'.
Re^2: Config::Simple with vars method
by Anonymous Monk on Jul 06, 2004 at 05:06 UTC
    David,
    I follow your info, but the CPAN page says: Quote
    Return value is one of "ini", "simple" or "http".
    Unquote
    when mentioning the 'guess_syntax' method. As my file is simple, it should recognise it as such.
    It also shows what code to use for 'simple' and 'ini' style files: Quote
    my %Config = $cfg->vars(); print "Username: $Config{User}"; # If it was a traditional ini-file: print "Username: $Config{'mysql.user'}";
    where the first is for 'simple' files, which I want to use.
    Cheers
    Chris
      Chris,
      I think you need to read the documentation a little more closely. It reads:

      SIMPLE CONFIGURATION FILE

      "Simple syntax is what you need for most of your projects. These are, as the name asserts, the simplest. File consists of key/value pairs, delimited by nothing but white space."

      INI-FILE

      "These configuration files are more native to Win32 systems. Data is organized in blocks. Each key/value pair is delimited with an equal (=) sign. Blocks are declared on their own lines enclosed in (brackets):"

      Your not providing blocks does not make it a 'simple' file. The fact that you are using '=' as a key/value delimiter does make it an 'ini' file. If you want to use 'simple', the replace the '=' with whitespace.

      davidj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-19 04:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found