5.15. Move external call into function

Example 5-24. examples/intro/t14_calc.t

#!/usr/bin/perl
use strict;
use warnings;

use Test::Simple "no_plan";

open my $fh, "<", "calc.txt" or die $!;

while ( my $line = <$fh> ) {
    chomp $line;
    next if $line =~ /^\s*(#.*)?$/;

    my ( $exp, $res ) = split /\s*=\s*/, $line;

    ok( mycalc($exp) == $res, $exp );
}

sub mycalc {
    return `./mycalc @_`;
}

See the mycalc() function.

We could have also implemented this in a module called Mycalc.
In that case we would just write use Mycalc; and test the exported function.

use MyCalc;
use Test::Simple tests => 1;
ok(mycalc('2 + 3') == 5);