IPC::Run3 for testing binaries

This is an example of a Perl script which uses IPC::Run3 and Test::More to run a test on a C binary program. The binary here is called j2e.cgi and this tries to run it with a command-line option of the form -q word=%E3%81%95%E3%82%8B, as in

j2e.cgi -q word=%E3%81%95%E3%82%8B
The routine run_binary could also be used to send standard input and standard error. This program also demonstrates converting the outputs back into UTF-8 encoding, and setting up Test::More to use the "utf8" pragma for its outputs.

use warnings;
use strict;
use Test::More tests => 1;
use IPC::Run3;
use Encode 'decode_utf8';
my $base_dir = '/share/projects/j2e/c-version/';
my $binary = "$base_dir/j2e.cgi";
use utf8;
binmode Test::More->builder->output, ":utf8";
binmode Test::More->builder->failure_output, ":utf8";

run_binary ({query => 'word=%E3%81%95%E3%82%8B', expect => 'さる'});

sub run_binary
    my ($options) = @_;
    my $in = $options->{in};
    my $query = $options->{query};
    my $expect = $options->{expect};
    my @cmd = ($binary);
    if ($query) {
        push @cmd, ("-q", $query);
    my $out;
    my $err;
    run3 (\@cmd, \$in, \$out, \$err);
    $out = decode_utf8 ($out);
    $err = decode_utf8 ($err);
    ok ($out =~ /$expect/, "Expect $expect");

Copyright © Ben Bullock 2009-2021. All rights reserved. For comments, questions, and corrections, please email Ben Bullock (benkasminbullock@gmail.com) or use the discussion group at Google Groups. News about the site. / Privacy / Disclaimer