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

OK, after all these years, I'm finally learning how to create a proper Perl module (yeah, I'm lame). I stumbled upon Module::Starter along with Damian Conway's plugin for it. These seem like great tools for helping me get up and running quickly and doing testing and making sure I document my code.

After installing these two modules, I can now issue a command like module-starter MyModule in my personal module library at ~/perl/perl-lib, (which I have in @INC) and it will set up all the files and the directory structures for testing and packaging my module. Sweet!

But when I go edit the module that was automatically generated for me at ~/perl/perl-lib/MyModule/lib/MyModule and run tests, I get an error:

t/00.load.t ....... 1/1 # Failed test 'use MyModule;' # at t/00.load.t line 4. # Tried to use 'MyModule'. # Error: Can't locate MyModule.pm in @INC (you may need to instal +l the MyModule module) (@INC contains: /home/steve/perl/perl-lib /etc +/perl /usr/local/lib/x86_64-linux-gnu/perl/5.20.2 /usr/local/share/pe +rl/5.20.2 /usr/lib/x86_64-linux-gnu/perl5/5.20 /usr/share/perl5 /usr/ +lib/x86_64-linux-gnu/perl/5.20 /usr/share/perl/5.20 /usr/local/lib/si +te_perl .) at t/00.load.t line 4. # BEGIN failed--compilation aborted at t/00.load.t line 4.
Now I know I'm getting this error because MyModule.pm is in the MyModule/lib directory. While I could simply add the full path to the test files, I'm assuming there's a better way to handle this. Can someone please clue me in? Thanks.

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

Replies are listed 'Best First'.
Re: Running tests on modules generated by Module::Starter still in /lib directory
by choroba (Cardinal) on Feb 12, 2017 at 18:22 UTC
    What do you mean by "run tests"?

    Probably not the following, right?

    cd ~/perl/perl-lib/MyModule prove -l

    Adding -v for verbose might be handy, too.

    Update: Added the cd .

    ($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,

      The test files found at MyModule/*.t

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

        man prove |grep '\s\-l'

        ...output...

        -l, --lib Add 'lib' to the path for your tests (- +Ilib).

        Dave

Re: Running tests on modules generated by Module::Starter still in /lib directory
by kcott (Archbishop) on Feb 13, 2017 at 07:21 UTC

    G'day nysus,

    "... I'm finally learning how to create a proper Perl module ... Module::Starter along with Damian Conway's plugin for it. ... Can someone please clue me in? Thanks."

    The enitre process should be incredibly simple. I don't know if you did something odd initially; or if you're running things from the wrong directories; or something else. I did a complete run to show you what's needed and where all the directories and files are.

    ken@ganymede: ~/tmp $ mkdir starting_dir ken@ganymede: ~/tmp $ cd starting_dir ken@ganymede: ~/tmp/starting_dir $ cat ~/.module-starter/config author: Ken Cotterill email: kcott@cpan.org builder: ExtUtils::MakeMaker Module::Build plugins: Module::Starter::PBP template_dir: /Users/ken/.module-starter/PBP ken@ganymede: ~/tmp/starting_dir $ module-starter --module=TestForNysus Added to MANIFEST: Build.PL Added to MANIFEST: Changes Added to MANIFEST: lib/TestForNysus.pm Added to MANIFEST: Makefile.PL Added to MANIFEST: MANIFEST Added to MANIFEST: README Added to MANIFEST: t/00.load.t Added to MANIFEST: t/perlcritic.t Added to MANIFEST: t/pod-coverage.t Added to MANIFEST: t/pod.t Created starter directories and files ken@ganymede: ~/tmp/starting_dir $ ls -al total 0 drwxr-xr-x 3 ken staff 102 Feb 13 17:18 . drwxr-xr-x 6 ken staff 204 Feb 13 17:15 .. drwxr-xr-x 9 ken staff 306 Feb 13 17:18 TestForNysus ken@ganymede: ~/tmp/starting_dir $ cd TestForNysus/ ken@ganymede: ~/tmp/starting_dir/TestForNysus $ ls -al total 20 drwxr-xr-x 9 ken staff 306 Feb 13 17:18 . drwxr-xr-x 3 ken staff 102 Feb 13 17:18 .. -rw-r--r-- 1 ken staff 661 Feb 13 17:18 Build.PL -rw-r--r-- 1 ken staff 110 Feb 13 17:18 Changes -rw-r--r-- 1 ken staff 138 Feb 13 17:18 MANIFEST -rw-r--r-- 1 ken staff 737 Feb 13 17:18 Makefile.PL -rw-r--r-- 1 ken staff 1318 Feb 13 17:18 README drwxr-xr-x 3 ken staff 102 Feb 13 17:18 lib drwxr-xr-x 6 ken staff 204 Feb 13 17:18 t ken@ganymede: ~/tmp/starting_dir/TestForNysus $ perl Makefile.PL && make && make test Checking if your kit is complete... Looks good WARNING: Setting ABSTRACT via file 'lib/TestForNysus.pm' failed at /Users/ken/perl5/perlbrew/perls/perl-5.24.0t/lib/site_perl/5.24.0/ +ExtUtils/MakeMaker.pm line 755. Generating a Unix-style Makefile Writing Makefile for TestForNysus Writing MYMETA.yml and MYMETA.json cp lib/TestForNysus.pm blib/lib/TestForNysus.pm Manifying 1 pod document PERL_DL_NONLAZY=1 "/Users/ken/perl5/perlbrew/perls/perl-5.24.0t/bin/pe +rl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Ha +rness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/00.load.t ....... 1/1 # Testing TestForNysus v0.0.1 t/00.load.t ....... ok t/perlcritic.t .... skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. t/pod-coverage.t .. skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. t/pod.t ........... skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. All tests successful. Files=4, Tests=1, 2 wallclock secs ( 0.03 usr 0.01 sys + 0.47 cusr + 0.07 csys = 0.58 CPU) Result: PASS

    So, I hadn't edited the module first. The WARNING is because the POD only has the skeleton code:

    =head1 NAME TestForNysus -

    I added some text for the ABSTRACT:

    =head1 NAME TestForNysus - blah blah blah

    I find it easier to run make realclean between changes; so, back at the previous directory:

    ken@ganymede: ~/tmp/starting_dir/TestForNysus $ make realclean && perl Makefile.PL && make && make test rm -f ... list of files cleaned up ... Checking if your kit is complete... Looks good Generating a Unix-style Makefile Writing Makefile for TestForNysus Writing MYMETA.yml and MYMETA.json cp lib/TestForNysus.pm blib/lib/TestForNysus.pm Manifying 1 pod document PERL_DL_NONLAZY=1 "/Users/ken/perl5/perlbrew/perls/perl-5.24.0t/bin/pe +rl" "-MExtUtils::Command::MM" "-MTest::Harness" "-e" "undef *Test::Ha +rness::Switches; test_harness(0, 'blib/lib', 'blib/arch')" t/*.t t/00.load.t ....... 1/1 # Testing TestForNysus v0.0.1 t/00.load.t ....... ok t/perlcritic.t .... skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. t/pod-coverage.t .. skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. t/pod.t ........... skipped: Author test. To run: set $ENV{CPAN_TEST_A +UTHOR} to a TRUE value. All tests successful. Files=4, Tests=1, 0 wallclock secs ( 0.03 usr 0.02 sys + 0.45 cusr + 0.05 csys = 0.55 CPU) Result: PASS

    I always run the make parts in the same window. I have "set -o vi" in my '.bash_profile", so after the first

    make realclean && perl Makefile.PL && make && make test

    I just need 3 keystrokes to repeat those 4 commands:

    <Esc>k<Enter>

    While developing a module, I'd normally have 3 windows open at these directories:

    For the make commands
    ~/tmp/starting_dir/TestForNysus/
    For editing TestForNysus.pm
    ~/tmp/starting_dir/TestForNysus/lib/
    For editing *.t files
    ~/tmp/starting_dir/TestForNysus/t/

    — Ken

      Thank you so much. This should provide me with much more of a clue as to how to do this.

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

Re: Running tests on modules generated by Module::Starter still in /lib directory
by stevieb (Canon) on Feb 12, 2017 at 18:26 UTC

    How, and from which directory are you running your tests?

    make test from within the distribution's root directory will Do The Right Thing. If you are running individual tests (eg: perl t/00-load.t), you need to first do a make install first. In the distribution root dir:

    perl Makefile.PL make make test make install

    Thereafter, if you don't make any changes to your Makefile.PL and want to run test files individually, simply make install after any change to your library (.pm file):

    make install perl t/xx-frobnicate_tests.t

    On a side note, each to their own on how they use module-starter, but I like to have the software configure as much as possible for me:

    module-starter --module=My::Module \ --author="Steve Bertrand" \ --email=my@email.com \ --license=perl

    At the very end of the help screen (just type "module-starter" without any arguments), there's an example if you forget (less the license option):

    Example: module-starter --module=Foo::Bar,Foo::Bat \ --author="Andy Lester" --email=andy@petdance.com

      Hi stevieb,

      If you are running individual tests (eg: perl t/00-load.t), you need to first do a make install first.

      make install should not be necessary to run tests, otherwise you've got a chicken/egg problem; perl -Ilib t/foo.t should be enough. Update: or prove -l t/foo.t.

      Regards,
      -- Hauke D

        You're right. I should have said "you *can* do a make install first". It's just a habit for me to do it this way.

      I am running the tests using a nifty keystroke shortcut ;t from within VIM when editing MyModule/lib/MyModule.pm. The shortcut triggers a VIM script which automatically runs the tests found at MyModule/*.t.

      So if I follow you correctly, are you saying I should run make install before I run every test? Is that something I should automate as part of the VIM script. Please bear with me, I never got the whole Makefile thing (did I tell you I was lame?)

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

        Hi nysus,

        Did you try what choroba suggested? The way I run my tests for all my (pure-Perl) modules is prove -l from the command line from the base directory of the module. make should not be necessary unless you've got XS code that you need to compile, but even then, prove should be able to run all the tests (AFAIK you need prove -lb in that case).

        Hope this helps,
        -- Hauke D

        What exactly does the Vim shortcut run when executed? fwiw, tests should reside in MyModule/t/*.t. Also, you never specified which directory you are in when running the test command. You should be in the distribution's root dir.

        You don't need to make install to run tests. make test is a common way to run your suite (all tests), and it automatically knows where to look for your libraries. You only need to do things differently when running individual test files. make install works, but as haukex stated in an earlier reply, it isn't necessary. Both of the below will work all the same, without installing (granted you're in the dist's root dir as I've said):

        "perl -Ilib t/foo.t should be enough. Update: or prove -l t/foo.t."
Re: Running tests on modules generated by Module::Starter still in /lib directory
by BillKSmith (Monsignor) on Feb 13, 2017 at 22:51 UTC
    This seems like a good place to ask a related question. I hope no one objects. Is there a tool analogous to Module::Starter to prepare the distribution of a main program (.pl) and supporting files?
    Bill