Robin's Wiki
SearchWiki

Kallisti

What's here?

  • Home Page
  • WebMail
  • Photo Gallery
  • Webserver Stats
  • Recent Changes

Software

Stuff I have a hand in

  • iRATE Radio
  • Journal Generator
  • PostInject
  • NeurosDBM
  • eMusic/J

Personal

Home pages of people

  • Robin's Stuff
  • John Machin

Wiki

Using this site to its fullest

  • PmWiki Home Page
  • Wiki Sandbox
  • What's a Wiki?
  • Documentation Index
  1. Kallisti
    1. Home Page
    2. WebMail: Squirrel Mail
    3. Photo Gallery
    4. Webserver Stats

  1. Hosted
    1. Roleplaying Games
    2. OURS

  1. Personal
    1. Robin Sheat
    2. John Machin

  1. Software
    1. Journal Generator
    2. PostInject
    3. eMusic/J

  1. Wiki
    1. Wiki Sandbox
    2. What is a Wiki?
    3. Wiki Documentation
PerlTips/
ConfigurationOptionsFromModules
Read PageEdit PagePage AttributesPage HistoryUpload
Printable View

I have a program that has a bunch of default settings that I want to be able to selectivly override based on an external file, perhaps mentioned on the command line or something (i.e. no prior knowledge).

This also allows code to be inserted into the module to do something very domain specific.

To do this, declare the variables using our rather than my. For example:

subset of main.pl:

our $label = "default label";
our $optionalSub = undef;

And then the module can be:

config.pm:

$label = "test #3";
$optionalSub = \&theSub;
<:vspace>
sub theSub {
        # code goes here
}

The main program then can load this with:

subset of main.pl:

unless (my $ret = do $config) {
        die "couldn't parse $config: $@" if $@;
        die "couldn't do $config: $!"    unless defined $ret;
        die "couldn't run $config"       unless $ret;
}

The variables can then be used just as if they were defined locally, and the sub can be called like:

subset of main.pl:

if (defined($optionalSub)) {
        $optionalSub->(...options go here...);
}
Page last modified on March 26, 2009, at 12:54 PM

  1. ▲ Top ▲
  2. Edit:
  3. SideBar
  4. MenuBar
  5. BottomBar
  6. GroupHeader
  7. GroupFooter