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

Hi Monks,

I wanted to create a simple CPAN module and I was reading the Module::Starter and Module::Installer.

But my modules requires user input before it installs itself. For example, it will ask a username and software revision... With these inputs, the "Makefile" needs to create a file with the username and software revision in it. This will be part of the module installation.

To me it sounds simple and I just could not find the answer.
  • Comment on Building a CPAN module with User Inputs

Replies are listed 'Best First'.
Re: Building a CPAN module with User Inputs
by GrandFather (Saint) on Oct 28, 2008 at 04:11 UTC

    In a word: "Don't". Modules get installed in all manner of ways which preclude user interaction. If you require information such as that for customization, pop up some ui the first time the module is used to ask for it, then save the information away in some place relative to the place the module was installed (if you can) or in a user relative location (if you can find one). Note that where you can store such stuff will depend on the host OS and on various other things outside the control of your module.


    Perl reduces RSI - it saves typing
      I found this notes from the book of Perl Hacks by Damian Conway, specifically - Hack 17. Collect Configuration Information.

      Some code you write needs configuration information when you build and + install it. For example, consider a program that can use any of seve +ral optional and conflicting plug-ins. The user must decide what to u +se when she builds the module, especially if some of the dependencies + themselves have dependencies. When you run your tests and the code in general, having this informati +on available in one spot is very valuableyou can avoid expensive and +tricky checks if you hide everything behind a single, consistent inte +rface. How do you collect and store this information? Ask the user, and then +write it into a simple configuration module!


      Then he presents his code which he describes as extending the Module::Build. I tried to run his code but was getting errors -- "Can't locate SUPER.pm in @INC..." . I wonder if there is a simpler version of implementing what Damian Conway mentioned in his book.

        The situation Damian describes as an example is somewhat different than your use appears to be. In the case Damian describes the information is required to tailor an installation including dependencies at install time. In your use case that does not seem to be the requirement. That is to say, the installed materials (except possibly some configuration constants) are not affected by the information that would be provided by the user at install time.

        It is better to avoid interrupting the install processing with required UI because that breaks automatic install processes. Where that requirement is known up front it's not so much of an issue, but if some module in the future requires your module to be installed the UI requirement could become rather troublesome. There's a lot of hand waving going on there of course and it could well be that the ifs and buts are not relevant or are a cost that you and users of your module are happy to accept.

        The ultimate answer is that your module is (conventionally) installed using makefile.pl to build a makefile that then is used to install and test your module. Guess what, the makefile.pl is a Perl script! You can edit it to do whatever you need. In the simplest case you could prompt for the required information then write that to a configuration file that is installed with the module.


        Perl reduces RSI - it saves typing
      Hmm... Sounds like my idea is not really that great after all.

      Thanks for your input.
      username/passwords don't usually belong in modules
Re: Building a CPAN module with User Inputs
by ikegami (Patriarch) on Oct 28, 2008 at 04:37 UTC

    A module that can only be used with one account? Isn't that something you should pass as parameters to the module when you use it? Having the application provide the credentials to the module allow the credentials to be collected using a method appropriate to the application (gui dialog, console input, config file, command line parameter, etc).

      Well, he didn't say the module was only to be used with one account. There's nothing wrong (in fact, it's something I think that should be done) with modules (and programs) having defaults. I really detest programs that refuse to run before you first write a configuration file.

        I really detest programs that refuse to run before you first write a configuration file.

        But you're ok with doing it when installing a module? The exact same prompt you'd get from the module can be given by the program.

Re: Building a CPAN module with User Inputs
by Anonymous Monk on Oct 28, 2008 at 14:04 UTC
    You would use the prompt() function ExtUtils::MakeMaker like
    my $value = prompt($message, $default);
    which won't interfere with non-interactive installs (for which default is taken)
Re: Building a CPAN module with User Inputs
by JavaFan (Canon) on Oct 28, 2008 at 08:12 UTC
    I suggest you rephrase your question, otherwise you only get replies ("don't do that, that's against the unwritten rules of our religion") which don't help you at all.

    Ask for instance "When Makefile.PL is run, I want to ask the user whether only the module should be installed, or also some optional scripts".

      Just be careful. While asking a more specific question may give you a more specific answer, it may not give you the right answer for the general question. That's what discussions are all about.
        Except that in this case, his real question "how do I ask something from the user when installing a module" still hasn't been answered - everyone is focussing on an irrelevant detail.

        Also, I didn't suggest to him to ask a more detailed question. I just suggested to him to change some details to not trigger responses that don't help him to answer his question.

Re: Building a CPAN module with User Inputs
by herveus (Prior) on Oct 28, 2008 at 14:27 UTC
    Howdy!

    Find some modules that have an interactive Makefile.PL (or Build.PL, I suppose) and see how they do it. LWP comes to mind as one example. DBI asks a bunch of questions, too, if I recall correctly.

    yours,
    Michael