Actions Status MetaCPAN Release

NAME

Command::Run - Execute external command or code reference

SYNOPSIS

use Command::Run;

# Simple usage
my $result = Command::Run->new(
    command => ['ls', '-l'],
    stderr  => 'redirect',  # merge stderr to stdout
)->run;
print $result->{data};

# Method chaining style
my $runner = Command::Run->new;
$runner->command('cat', '-n')->with(stdin => $data)->run;
print $runner->data;

# Separate stdout/stderr capture
my $result = Command::Run->new(
    command => ['some_command'],
    stderr  => 'capture',
)->run;
print "data: ", $result->{data};
print "error: ", $result->{error};

# Access output via file descriptor path
my $cmd = Command::Run->new(command => ['date']);
$cmd->update;
system("cat", $cmd->path);  # /dev/fd/N

# Code reference execution
my $result = Command::Run->new(
    command => [\&some_function, @args],
    stdin   => $input_data,
)->run;

# Using with() method
my ($out, $err);
Command::Run->new->command("command", @args)
    ->with(stdin => $input, stdout => \$out, stderr => \$err)
    ->run;

VERSION

Version 0.9902

DESCRIPTION

This module provides a simple interface to execute external commands or code references and capture their output.

When a code reference is passed as the first element of the command array, it is called in a forked child process instead of executing an external command. This avoids the overhead of loading Perl and modules for each invocation.

This module inherits from Command::Run::Tmpfile, which provides temporary file functionality. The captured output is stored in this temporary file, accessible via the path method as /dev/fd/N, which can be used as a file argument to external commands.

CONSTRUCTOR

PARAMETERS

The following parameters can be used with new, with, and run. With new and with, parameters are stored in the object. With run, parameters are temporary and do not modify the object.

METHODS

RETURN VALUE

The run method returns a hash reference containing:

COMPARISON WITH SIMILAR MODULES

There are many modules on CPAN for executing external commands. This module is designed to be simple and lightweight, with minimal dependencies.

This module was originally developed as App::cdif::Command and has been used in production as part of the App::cdif distribution since 2014. It has also been adopted by several unrelated modules, which motivated its release as an independent distribution.

Command::Run differs from these modules in several ways:

SEE ALSO

Command::Run::Tmpfile, IPC::Run, Capture::Tiny, IPC::Run3, Command::Runner

AUTHOR

Kazumasa Utashiro

LICENSE

Copyright 2026 Kazumasa Utashiro.

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