I've recently been trying to put together a module for distribution and it struck me that in the same way that using POD keeps the documentation together with the module, it would be useful if you could do the same thing with a test suite/program. It would also be useful if the test program was useable directly for install time verification.

I thought about various schemes of having a test function or method that could be invoked, which would be okay, but still require another script--even if it is only a -e one-liner to invoke the test code.

Then inspiration hit me and I remembered 2 things I had seen recently.

  1. A comment in the CB that a module doesn't need a shebang line as it isn't intended for direct extecution.
  2. -x command line switch that can be used to skip over junk at the top of a script until a shebang line is found and start executing there.

Putting the two together took a little experimentation, but the result is that you can construct a .pm file that acts in every way that I have tried as a normal module when invoked via either a use my::module; statement or a require 'my::module'; or even a one-liner using the -m switch.

However, if the module is run as a program using the -x switch, it will execute the embedded program, load itself as a module and function as a 'normal' script would.

A trivial example

package My::Module; sub new{ bless \rand, shift(); } sub do_something{ print 'Do what?'; } return 1; =head1 NAME My::Module - Test my idea. =head1 Synopsys/Example See demo/test code at the end of this module. =head1 Description Demo of idea to integrate a runnable test suite into a module =head1 Copyright Copyright 2003, by BrowserUK. All rights reserved. May be used, copied, altered or sold freely under the same terms a +nd conditions as Perl itself. =head1 Demo/test program. The following code serves as both demo program and test suite. To run the integrated version switch to the directory where the mo +dule is located and type: =begin text perl -x Module.pm =end text The program should respond with: =begin text E:\Perl\site\lib\My>perl -x Module.pm Do what? E:\Perl\site\lib\My> =end =cut #! perl use strict; # use My::Module; # Use this instead of the following line in your cod +e. unshift @INC, '.'; require $0; my $test = My::Module->new(); $test->do_something();

Is this a useful idea? Stupid?


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.


In reply to RFC: Runnable test code integrated into Modules. by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.