Fearfully asking my first question at the Monastery:
Reading the various nodes on configuration files has prompted me to change my current config usage(primarily 'require'ing in files). I've decided to create a module to abstract my usage of XML-based config files. (If you're curious about why I am 're-inventing the wheel' in this way, I'll be glad to explain.)
I have two questions which I hope someone here can help me with. First, here is what I have so far:
package SiteConfig;
use strict;
use XML::Simple;
BEGIN {
use vars qw( @configs );
@configs = qw( site search survey editor );
}
use vars qw( @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT );
require Exporter;
@ISA = qw(Exporter);
%EXPORT_TAGS = ( );
@EXPORT = qw( );
@EXPORT_OK = map ('%'.$_, @configs2);
# Config Starts Here
use vars map ('%'.$_, @configs2);
# Import config files
%site = %{ XMLin('./site.xml') };
%search = %{ XMLin('./search.xml') };
%survey = %{ XMLin('./survey.xml') };
%editor = %{ XMLin('./editor.xml') };
1;
My intent is to be able to use the module as follows:
use SiteConfig qw( %site %search );
importing in only the config hashes I need for that app.
My
first (and most important)
question is: how do I do the final import section without explicitly listing each hash, and
without using symbolic refs?
I know, for example, that I could do this:
no strict;
for (@configs)
{
%{$_} = %{ XMLin("./$_.xml") };
}
but I also know that in most cases that is a taboo. Is there an alternative that will allow me to keep my clean and simple interface without resorting to evil?
Second question: It's obviously a waste of resources to import all of the XML files for an application that will only be using one or two config hashes. Is there someway to trick the module into only loading the files when I import the associated hash?
Thanks,
Impossible Robot
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.