The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Command::Runner - run external commands and Perl code refs

SYNOPSIS

  use Command::Runner;

  my $cmd = Command::Runner->new(
    command => ['ls', '-al'],
    timeout => 10,
    on => {
      stdout => sub { warn "out: $_[0]\n" },
      stderr => sub { warn "err: $_[0]\n" },
    },
  );
  my ($status, $is_timeout) = $cmd->run;

  # you can also use method chains
  my $ret = Command::Runner->new
    ->command(sub { warn 1; print 2 })
    ->redirect(1)
    ->on(stdout => sub { warn "merged: $_[0]" })
    ->run;

DESCRIPTION

Command::Runner runs external commands and Perl code refs

METHODS

new

A constructor, which takes:

command

arrays of external commands, strings of external programs, or Perl code refs

CAUTION! Currently this module does NOTHING for quoting. YOU are responsible to quote argument lists. See Win32::ShellQuote and String::ShellQuote.

timeout

timeout second. You can set float second.

redirect

if this is true, stderr redirects to stdout

on.stdout, on.stderr

code refs that will be called whenever stdout/stderr is available

run

Run command. It returns ($status, $is_timeout) in list context, and $status in scalar context.

MOTIVATION

I develop a CPAN client App::cpm, where I need to execute external commands and Perl code refs with:

timeout
flexible logging
high portability

While App::cpanminus has excellent APIs for such use, I still needed to tweak them in App::cpm.

So I ended up creating a seperate module, Command::Runner.

AUTHOR

Shoichi Kaji <skaji@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2017 Shoichi Kaji <skaji@cpan.org>

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.