Somewhat off the top of my head ...

package My::App; # ... require Config::IniHash; sub _configuration { my $self = shift; unless ($self->{_configuration}) { require Config::Find; $self->{_config_name} = Config::Find->find(name => 'myapp', mode => 'read'); if ($self->{_config_name} and -e $self->{_config_name}) { $self->{_configuration} = $self->_default_configuration(); } else { $self->{_configuration} = Config::IniHash->new($self->{_config_n +ame}); } } $self->{_configuration}; } sub _default_configuration { my $self = shift; unless ($self->{_default_configuration}) { # Config::IniHash docs don't say that it can take # a glob, but the code does. $self->{_default_configuration} = Config::IniHash->new(\*DATA); } $self->{_default_configuration}; } __DATA__ # defaults go here. [section1] key1=default value 1 key2=default value 2 [section2] key1=default value 2-1

Because of the way that IniHash works, you may have to do something like this to use it:

my $value = $self->_configuration()->{section1}->{key1} || $self->_default_configuration()->{section1}->{key1};

Off the top of my head, I can't think how to combine them. Good luck!

PS - looking at the code I have this in, I'm using Config::Natural, so I have a sub that is "get_config", and I pass in the config var I want, and it does the work of checking the config file vs the DATA (defaults).


In reply to Re^3: backup storage for variables by Tanktalus
in thread backup storage for variables by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.