File Coverage

blib/lib/POE/Component/ControlPort/Command.pm
Criterion Covered Total %
statement 29 29 100.0
branch 5 6 83.3
condition n/a
subroutine 6 6 100.0
pod 0 2 0.0
total 40 43 93.0


line stmt bran cond sub pod time code
1             # $Id: Command.pm 222 2004-04-24 17:55:11Z sungo $
2              
3             =pod
4              
5             =head1 NAME
6              
7             POE::Component::ControlPort::Command - Register control port commands
8              
9             =head1 SYNOPSIS
10              
11             use POE::Component::ControlPort::Command;
12              
13             POE::Component::ControlPort::Command->register(
14             name => 'test',
15             topic => 'sample_commands',
16             usage => 'test [ text to echo ]'
17             help_text => 'test command. will echo back all parameters',
18             command => sub { my %args = @_; return join(" ", @{$args{args}}); }
19             );
20              
21             =head1 DESCRIPTION
22              
23             This module has one command for public consumption. C is the
24             way that one registers commands for use in the control port. The
25             arguments listed in the synopsis are all the available arguments and are
26             all mandatory.
27              
28             =cut
29              
30             package POE::Component::ControlPort::Command;
31              
32 2     2   11057 use warnings;
  2         4  
  2         59  
33 2     2   11 use strict;
  2         3  
  2         62  
34              
35 2     2   9 use Carp;
  2         3  
  2         155  
36 2     2   2617 use Params::Validate qw(:all);
  2         32611  
  2         1554  
37              
38             our $VERSION = do { my @r= (q|$Revision: 1.3 $| =~/\d+/g); sprintf "%d."."%04d"x$#r, @r };
39              
40             our %TOPICS;
41              
42             our %REGISTERED_COMMANDS;
43              
44              
45             sub register {
46 8     8 0 8115 my $class = shift;
47              
48 8         1095 my %args = validate( @_, {
49             help_text => { type => SCALAR },
50             usage => { type => SCALAR },
51             topic => { type => SCALAR },
52             name => { type => SCALAR },
53             command => { type => CODEREF },
54             } );
55              
56 5         42 push @{ $TOPICS{ $args{topic} } }, $args{name};
  5         23  
57 5         18 $REGISTERED_COMMANDS{ $args{name} } = \%args;
58              
59 5         23 return 1;
60             }
61              
62             sub run {
63 8     8 0 13106 my $class = shift;
64              
65 8         726 my %args = validate( @_, {
66             command => { type => SCALAR },
67             oob_data => { type => HASHREF, optional => 1 },
68             arguments => { type => ARRAYREF, optional => 1 },
69             } );
70              
71 5 100       43 if($REGISTERED_COMMANDS{ $args{command} }) {
72             return "Bad command '$args{command}" unless
73 4 50       22 ref $REGISTERED_COMMANDS{ $args{command} }{ command } eq 'CODE';
74              
75 4         7 my $txt = eval {
76 4         13 &{$REGISTERED_COMMANDS{ $args{command} }{ command }}(
  4         19  
77             args => $args{arguments},
78             oob => $args{oob_data},
79             );
80             };
81            
82              
83 4 100       45 if($@) {
84 1         4 return "ERROR: $@";
85             } else {
86 3         14 return $txt;
87             }
88             } else {
89 1         7 return "ERROR: '$args{command}' is unknown.";
90             }
91             }
92              
93              
94             1;
95             __END__