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.
- A comment in the CB that a module doesn't need a shebang line as it isn't intended for direct extecution.
- -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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.