Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Configure global default variables in package at install time

by zachhh (Initiate)
on May 21, 2013 at 13:30 UTC ( #1034531=perlquestion: print w/ replies, xml ) Need Help??
zachhh has asked for the wisdom of the Perl Monks concerning the following question:

I don't know if this is wise, but I'd like to be able to set global default variables for a package at build time. The perl module is a back end designed to communicate with a JavaScript front end (see https://github.com/zachhh/Packform). The global variables are for things like the path (as in the href attribute of a script tag, not strictly the path on disk) for including the front end JS, the desired version of jQuery and the jQuery UI, etc.

This is entirely out of laziness -- I don't want to have to provide this information in every script using the module. The defaults are hardcoded to be suitable for the server on which it was built. I want to be able to specify these when I install it.

This would necessarily have to change the variable initializations in the .pm file (which seems dangerous -- but hey, make test will tell them something's wrong if they mess it up). Of course, this could also come from a configuration file or the like. But, that seems dirty too.

Is this approach unwise?

If this is a reasonable approach, what are the recommendations? Should this be done in Makefile.PL somehow? Should there be a conf file that is provided and ask the user to modify that if they want changes? How should that be incorporated?

Comment on Configure global default variables in package at install time
Re: Configure global default variables in package at install time
by vsespb (Hermit) on May 21, 2013 at 13:48 UTC
    I would use configuration file instead. Path to configuration file can be:
    • some global known path like /etc
    • home dir subdirectory, example: ~/.myapp
    • Path relative to main application .pl file location - see Perl FindBin core module
    UPD

    But if you want to modify .pm at build time - at least, please, write another .pm file from scratch. (like MyConfig.pm) it should contain only variables, nothing except that.
Re: Configure global default variables in package at install time
by pokki (Scribe) on May 21, 2013 at 14:37 UTC

    I've done this (semi-automatically) for a module before. Module::Build provides a way to save config data (see here). Then you can access the configuration by use-ing Foo::Bar::ConfigData and calling Foo::Bar::ConfigData->config('foo');. During build, you can also write config there ($builder->config(foo => 'blah');) whenever you want. I'm sure other packaging systems have similar features.

    But if you're going this way, do consider writing a regular, user-editable config file instead. It will make your life easier when deploying. More importantly, it will make the devops' life easier. Configuration done via .pm files tends to be hard to modify by regular users.

    It would also allow your module to be able to run with different instances of the front end, by providing the paths to different config files, something that is harder to do with a .pm file.

Re: Configure global default variables in package at install time (File::UserConfig )
by Anonymous Monk on May 21, 2013 at 21:41 UTC

    If this is a reasonable approach, what are the recommendations? Should this be done in Makefile.PL somehow? Should there be a conf file that is provided and ask the user to modify that if they want changes? How should that be incorporated?

    File::HomeDir + File::ShareDir => File::UserConfig

    You can http://search.cpan.org/perldoc/ExtUtils::MakeMaker#PL_FILES to generate sharedir/ThisMachineDefaults

    Then in your module/program use File::UserConfig, so users get an .appdir with ThisMachineDefaults copied into it, which they can customize ...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034531]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2014-09-17 02:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (56 votes), past polls