There are cases when you have some code using fork the question arises how to test them.

Here is a simple example:

The application that is forking:

examples/test-fork/MyApp.pm

package MyApp;
use strict;
use warnings;
use File::Temp qw(tempdir);

sub logger {
    my ($msg) = @_;
    print "$msg\n" if $ENV{DEBUG};
}

sub work {
    my ($n) = @_;

    logger("work: $$");
    my $dir = tempdir(CLEANUP => 0);
    my $path = File::Spec->catfile($dir, 'result');

    my $pid = fork();
    die "Could not fork $!" if not defined $pid;
    if ($pid) {
        parent($pid, $dir, $path);
    } else {
        child($n, $path);
    }
}

sub parent {
    my ($child_pid, $dir, $path) = @_;
    logger("parent: $$ waiting for child $child_pid");
    my $finished = wait;
    logger("done: in $$ (finished child $finished)");
    open my $fh, '<:encoding(utf8)', $path or die;
    my $result = <$fh>;
    return $result;
}

sub child {
    my ($n, $path) = @_;
    logger("child($n): $$");

    my $result = 2 * $n;
    if (open my $fh, '>:encoding(utf8)', $path) {
        print $fh $result;
    }
    exit;
}


1;

The code to use it:

examples/test-fork/use_my_app.pl

use strict;
use warnings;
use MyApp;

print MyApp::work(21), "\n";

Run it as

perl -I. use_my_app.pl

To turn on debug printing run it like this:

DEBUG=1 perl -I. use_my_app.pl

Here is a test script:

examples/test-fork/test.t

use strict;
use warnings;
use MyApp;

use Test::More;

my $out = MyApp::work(21);
is $out, 42;

done_testing;

Run it as:

prove -I. test.t