We are going to create a module called SelfTestingCalc.pm and a script called use_self_testing_calc.pl.
The latter looks like any other Perl script. It loads a module importing a function and then calls that function.
Nothing special:

examples/use_self_testing_calc.pl

use 5.010;
use strict;
use warnings;
use SelfTestingCalc qw(add);
say add(2, 5)

If we run this script we see the result printed on the screen:

$ perl use_self_testing_calc.pl
7

On the other hand, if we try to "run" the module we see the same Test Anything output as we would see if we ran a unit test.
Including potential test failures:

The first part of the module is exactly the same as we would write any "normal" module.
Well, maybe except of the fact that I added a deliberate error case in the add function
just so I can show you a test failure.

The difference is that we also have a self_test function and then we call this function
at the bottom of our module if it is executed as a script

In the self_test function we write almost exactly the same as we would write in an external
test file discussed in the testing series.

There are two differences:

One is that we load the module using the run-time require and the
import, instead of the compile-time use statement.

The other is that we have to put parentheses after each one of the functions imported from the Test::More module such as plan, ok, is, like, ...

Why write self testing modules?

Just as having documentation embedded in the module in pod format will ensure that the code is not distributed without documentation, having test-code embedded in the module will also make sure you can always test the module.

Despite this advantage of course the need to compile this extra code on every invocation of the module might make some people feel uncomfortable and this does not provide a clear suggestion where to put cross-module tests, but this certainly can be an option. Especially in projects where the directory layout is different from the "standard Perl project layout".

More about self testing Modulinos

Read more about self testing Modulionos in
Mastering Perl by brian d foy who coined the term.