The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MooseX::Role::Cmd - Wrap system command binaries the Moose way

SYNOPSIS

Create your command wrapper:

package Cmd::Perl;

use strict;
use warnings;

use Moose;

with 'MooseX::Role::Cmd';

has 'e' => (isa => 'Str', is => 'rw');

# other perl switches here...

1;

Use it somewhere else:

use Cmd::Perl;

my $perl = Cmd::Perl->new(e => q{'print join ", ", @ARGV'});
print $perl->run(qw/foo bar baz/);
# prints the STDOUT captured from running:
# perl -e 'print join ", ", @ARGV' foo bar baz

DESCRIPTION

MooseX::Role::Cmd is a Moose role intended to ease the task of building command-line wrapper modules. It automatically maps Moose objects into command strings which are passed to IPC::Cmd.

ATTRIBUTES

$cmd->bin_name

Sets the binary executable name for the command you want to run. Defaults the to last part of the class name.

$cmd->stdout

Returns the STDOUT buffer captured after running the command.

$cmd->stderr

Returns the STDERR buffer captured after running the command.

METHODS

my $bin_name = $cmd->build_bin_name

Builds the default string for the command name based on the class name.

my @stdout = $cmd->run(@args);

Builds the command string and runs it based on the objects current attribute settings. This will treat all the attributes defined in your class as flags to be passed to the command.

Suppose the following setup:

has 'in'  => (isa => 'Str', is => 'rw')
has 'out' => (isa => 'Str', is => 'rw');

# ...

$cmd->in('foo');
$cmd->out('bar');

The command will be invoked as:

cmd -in foo -out bar

All quoting issues are left to be solved by the user.

cmd_args

Returns a list of the computed arguments that will be added to the command

PRIVATE METHODS

_attr_name_to_cmd_options

Returns an array (or array reference) of command options that correspond to the given attribute name. This essentially provides the following functionality:

Bool as flags, everything else as key => value params

has 'v'       => ( isa => 'Bool' );                 # -v
has 'i'       => ( isa => 'Str' );                  # -i input.data

Use Getopt::Long conventions with hyphenations:

has 'v'       => ( isa => 'Bool' );                 # -v
has 'verbose' => ( isa => 'Bool' );                 # --verbose

Use optional info from CmdOpt trait:

has 'v'       => ( isa          => 'Bool' );        # -v

has 'short'   => ( traits       => [ 'CmdOpt' ],
                   isa          => 'Bool',
                   cmdopt_prefix => '-'
                 );                                 # -short

has 'rename'  => ( traits       => [ 'CmdOpt' ],
                   isa          => 'Bool',
                   cmdopt_name  => '+foo'
                 );                                 # +foo

AUTHOR

Eden Cardim <edencardim@gmail.com>

LICENSE

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