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

Tools for configurator script?

by nysus (Parson)
on Jan 23, 2018 at 21:31 UTC ( [id://1207782]=perlquestion: print w/replies, xml ) Need Help??

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

I'm creating a module that is a wrapper to a CLI tool. When the module first installs, I want it to prompt the user for a configuration setting and then save it in a configuration file. What tools/modules will make this easy? And what do I have to be careful of?

Also, a secondary question I have is, since this module will have a CLI, it will require a simple bin command to pass the commands to the module (which will then pass them through to the actual CLI too. What's the best way for the perl module to install this bin script to their machine? Is there anyway to avoid requiring the user to have to modify their .bash profile? Update: thanks for all the help! This will save me a lot of time.

$PM = "Perl Monk's";
$MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate Priest";
$nysus = $PM . ' ' . $MCF;
Click here if you love Perl Monks

Replies are listed 'Best First'.
Re: Tools for configurator script?
by choroba (Cardinal) on Jan 23, 2018 at 22:14 UTC
    Asking the user during installation isn't a good idea. Modules tend to be installed automatically by various installation and deployment scripts, user interaction can't be guaranteed. Maybe require a config file somewhere (in the home directory?) and die if it's not present, or create a default and warn.

    To install an executable, list it under EXE_FILES in the Makefile.PL (or script_files in Build.PL). The standard location of such a script is bin/ or script/ directory of the distribution. Click "Browse" in App::Ack or App::ph.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Tools for configurator script?
by hippo (Bishop) on Jan 23, 2018 at 22:16 UTC
    I want it to prompt the user for a configuration setting and then save it in a configuration file. What tools/modules will make this easy?

    Config::General should suffice.

    And what do I have to be careful of?

    Not writing enough tests.

    What's the best way for the perl module to install this bin script to their machine?

    ExtUtils::MakeMaker will handle this.

    Is there anyway to avoid requiring the user to have to modify their .bash profile?

    Yes - install the binary into an existing entry in the user's $PATH.

Re: Tools for configurator script?
by davido (Cardinal) on Jan 24, 2018 at 06:43 UTC

    ExtUtils::MakeMaker has a prompt function that allows you to pre-set a default, but accept user input. If EU::MM detects that installation is occurring in an environment that is not interactive, the default will be selected.

    Additionally, if you go the Makefile.PL route and use ExtUtils::MakeMaker, you can do whatever environmental assessments you need within the Makefile.PL so that the makefile can be generated with defaults tailored to the target environment. EU::MM also provides hooks into other stages in the build.

    The documentation for EU::MM also suggests how to handle executables that need to be installed as part of the make process. So a dive into that documentation will help. You can also look over well-known distributions such as Mojolicious for examples of the make process spitting out some executables into proper paths.

    In short, the CPAN module toolchain is powerful and geared toward adaptability to a wide range of environments. Read through the documentation for EU::MM, and follow some of the links to other documents that its POD provides.


    Dave

Re: Tools for configurator script?
by stevieb (Canon) on Jan 23, 2018 at 22:45 UTC

    To further what has already been said, you can take a look at this basic Makefile.PL at the EXE_FILES directive to see how to go about having "binary" files installed. In that case, the bin/ directory is off of the root of the distribution. It's simply an array reference. For numerous files, you can simply use basic Perl idioms:

    EXE_FILES => [ map "bin/$_", qw(brewbuild bbtester bbdispatch) +],

    Also, I'm in agreement with choroba that during testing/install, you do not want to prompt the user if at all possible. Having a config file available in advance is a good idea (and skip tests if its not there), or have a default config to use in the event the user doesn't supply their own. Here's another Makefile.PL that has some conditionals, including copying a configuration file into place prematurely. Here's an example in a Makefile.PL where I die() if a custom prerequisite isn't met (it's not a config file, but an actual application installation). No tests run, no installation happens, all is good in the world.

    if (! grep { -x "$_/sqlite3" } split /:/, $ENV{PATH}) { die "sqlite3 must be installed to continue\n"; }

    In a couple of cases, I go as far as to write a module that I have in the t/ directory that handles config file and database creation amongst other things that I load into certain test files, and use the functions within as-needed on a test-file basis. Example module and here's a test file that uses that module, and a few functions within it (set up config, set up db, remove config and destroy db).

    Last, but not least, if you can test and install without this config file, you can simply skip the tests that don't meet the criteria you need for these tests, and again, the world will be ok (replace the env var not set for a missing test config file for example). Tests can be skipped individually (see Test::More) or you can skip the whole test file in its entirety:

    if (! $ENV{RPI_SERIAL}){ plan skip_all => "RPI_SERIAL not set; Not running RPI::Serial test +s\n"; exit; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-26 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found