Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Lazy config files?

by bsb (Priest)
on Aug 28, 2003 at 04:25 UTC ( [id://287236]=perlquestion: print w/replies, xml ) Need Help??

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

In the myriad of Config modules, is there anything that easily allows lazy loading of config sections?
(or subfiles?)

I'd like this so that utility scripts can just use the database info, or just the TT2 options, or just the SQL table definitions.

There are two reasons I'm interested in laziness
1) Installation and admin scripts using partial configs
2) Heavy, complex object structures not required in many paths (a schema structure)

Brad

Replies are listed 'Best First'.
Re: Lazy config files?
by BrowserUk (Patriarch) on Aug 28, 2003 at 06:58 UTC

    If you don't find a module that fits the bill, this might.

    #! perl -slw package Tie::Hash::Config; use Carp; use Data::Dumper; use Tie::Hash; my %fhs; sub TIEHASH { die "Usage: tie my \%config, '\$class', filename or handle" unless @_ == 2; my( $class, $config ) = @_; open $config, '<', $config or die "Can't open $config: $!" unless ref $config eq 'GLOB'; my $self = bless {}, $class; $fhs{ $self } = $config; return $self; } sub FETCH{ my( $self, $key ) = @_; return $self->{ $key } if exists $self->{ $key }; my $fh = $fhs{ $self }; seek $fh, 0, 0; local $/ = ''; # paragraph mode my %section; while( <$fh> ) { m/ ^ \[ ( [^]]+ ) \] \s* /gcsx and $1 eq $key or next; ($section{ $1 } = $2) =~ s[\s*$][]s while m[ ^ ( [^=]+? ) \s* = \s* ( .+? ) (?= \Z | (?: ^ [^=\s]+ \s* = ) ) ]smxg; } return $self->{ $key } = \%section; } return 1 if caller; package main; use Data::Dumper; scalar <DATA>; seek DATA, 0, 0; tie my %config, 'Tie::Hash::Config', \*DATA; print Dumper $config{ "section$_" } for 1 .. 4; __DATA__ [section1] key1 = value1 key2 = value 2 key3 = value 3 [section2] key1 = value1 key2 = value2 some more stuff here key3 = value3 [section3] key1 = value1 key2 = value2 key3 = This is a whole paragraph of value the only restriction is that it mustn't contain an equals sign (=) surrounded by whitespace. [section4] key 1 = keys can contain spaces too. key2=value2 key3= =

    To try it out, download the code and run it as a script or put it into site\Tie\Hash\Config.pm and use it from your own script as

    use Tie::Hash::Config; tie my %config, 'Tie::Hash::Config', 'your.cfg'; print $config{section}{key}; # or my $section = $config{section}; print "$key => $value\n" while my($key, $value) = each %( $section }; # etc

    Sections are loaded on demand.

    If it is of use and interest, I could clean it up and document it for posting.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Lazy config files?
by valdez (Monsignor) on Aug 28, 2003 at 05:21 UTC

    May I suggest Config::General? It can include external files and its syntax is similar to Apache's conf files.

    HTH, Valerio

Re: Lazy config files?
by broquaint (Abbot) on Aug 28, 2003 at 09:06 UTC
    If you're looking for real laziness then Config::Auto may be the module for you. With a call as simple as Config::Auto::parse() you could have yourself a nice hash representing your configuration file!
    HTH

    _________
    broquaint

      I meant the other kind of laziness. :)

      Thanks anyway.

Re: Lazy config files?
by Ryszard (Priest) on Aug 28, 2003 at 08:36 UTC
    For my config files i use a perl module, then dereference the data structure. It may become difficult to use when scaled excessively, but works well in my situation.
    Package Attr; use strict; use vars qw(%struct); %struct = ( foo => 'bar'. baz => { blah => 1, } ); 1
    and to use it:
    #!/usr/bin/perl -w use strict; use Attr; use vars qw(%struct); *struct = \%Attr::struct;
      Yeah, I kinda like evalling a file instead of parsing names and values. It leaves the design open, you can put custom behavior in there as well as just values.

      I might try to mix this approach with Data::Lazy.

      Ta

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-25 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found