MooseX::Role::Cmd.3pm

Langue: en

Autres versions - même langue

Version: 2008-12-05 (fedora - 05/07/09)

Section: 3 (Bibliothèques de fonctions)

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


$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


$cmd->stdout

Returns the STDOUT buffer captured after running the command.

$cmd->stderr


$cmd->stderr

Returns the STDERR buffer captured after running the command.

METHODS


my $bin_name = $cmd->build_bin_name


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);


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.