Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Config::Std blank entry in hash, and blank line

by carcassonne (Pilgrim)
on Dec 14, 2005 at 13:46 UTC ( [id://516639]=perlquestion: print w/replies, xml ) Need Help??

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

Howdy,

Using the configuration file below I obtain a blank entry when listing all the keys in %config. Also, when adding a new section, a blank line separates the section name from the entry of that section, like this:

[FifthBlock] build: Test_2.00.002

Here's the test code:

read_config 'test2.cfg' => my %config; print Dumper(%config); $config{"FifthBlock"}{"build"} = "Test_2.00.002"; write_config %config, "test2.modified"; exit 0;

And here's the configuration file (platform is Linux - i.e. only newlines):

[MainSection] branch: FirstBlock branch: SecondBlock branch: ThirdBlock branch: FourthBlock [MainSectionBuilds] build: TestBuild-001 build: TestBuild-002 build: TestBuild-003 build: TestBuild-004 build: TestBuild-005 [FirstBlock] build: Test_1.04.001 build: Test_1.04.002 build: Test_1.04.003 build: Test_1.04.004

And here's the beginning of Dumper:

$VAR1 = ''; $VAR2 = {}; $VAR3 = 'FirstBlock'; $VAR4 = { 'build' => [

$VAR1 and $VAR2 are holding nothing. Does this have an effect on the extra newline between added section name and section entry ?

Replies are listed 'Best First'.
Re: Config::Std blank entry in hash, and blank line
by TheDamian (Vicar) on Dec 14, 2005 at 19:12 UTC
    Using the configuration file below I obtain a blank entry when listing all the keys in %config.
    That's because every config file starts with the "nameless" section. As soon as you name a section, it shifts to that section instead but, even if your first line is a section marker, there will still be an entry for the nameless section (which had no data) in the resulting hash.

    Arguably however, that's unintuitive, and the nameless section ought not appear in the resulting hash unless it has actual contents. I'll patch the next release to work that way.

    Also, when adding a new section, a blank line separates the section name from the entry of that section
    Yes, that's the standard behaviour. And, no, there's currently no way of altering it. I guess read_config could look at the input file's layout and intuit whether you're separating section markers from config variables. I'll look at adding that behaviour too.

    Damian

      I guess read_config could look at the input file's layout and intuit whether you're separating section markers from config variables.

      Maybe just adding a configurable parameter like 'noExtraNewlines' that would put newlines only in-between sections when adding entries ? That is, the current scheme of the user's file use of newlines would be preserved. Only new entries would be adopting the configurable option. Concern is, many users coudl eventually modify the same INI file, each one adding newlines when he/she sees it fit.

      How about then an INI file formatter that would straigthen this out according to a defined (and configurable) scheme ? ;-)

        I would prefer not to add any more configuration parameters. The idea here is simplicity and avoiding too many variations (hence ::Std ;-)

        An INI formatter would be a great idea, but this module isn't (going to be) that.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://516639]
Approved by Corion
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 13:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found