File Coverage

bin/rcon-minecraft
Criterion Covered Total %
statement 47 47 100.0
branch 18 18 100.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 74 74 100.0


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 3     3   71739 use 5.008;
  3         19  
4 3     3   35 use strict;
  3         6  
  3         71  
5 3     3   13 use warnings;
  3         4  
  3         74  
6              
7 3     3   1246 use Net::RCON::Minecraft;
  3         290  
  3         112  
8 3     3   2341 use Getopt::Long qw< GetOptionsFromArray >;
  3         27919  
  3         13  
9 3     3   1990 use Pod::Usage;
  3         130976  
  3         396  
10 3     3   24 no warnings 'uninitialized';
  3         6  
  3         917  
11              
12             exit main(@ARGV) unless caller;
13              
14             sub main {
15 18     18   18267 my $ARGV = \@_;
16              
17 18         42 my %o = ( command => [ ] );
18 18 100       49 GetOptionsFromArray($ARGV, \%o, qw< host=s port=i password=s timeout=f
19             command|cmd=s@ color quiet echo help version > ) or return 2;
20 12 100       7827 if ($o{help}) { pod2usage -verbose => 1, -exitval => 'noexit'; return }
  1         4  
  1         6628  
21 11 100       19 if ($o{version}) { print "rcon-minecraft version 0.02\n"; return }
  1         57  
  1         7  
22              
23 10 100       16 push @{$o{command}}, join(' ', @$ARGV) if @$ARGV;
  6         14  
24              
25 10         166 $o{rcon} = Net::RCON::Minecraft->new(%o);
26 10         152 $o{rcon}->connect; # Not necessary, but better here to fail early
27              
28 9 100       21 if (@{$o{command}}) { for (@{$o{command}}) { run_command(\%o, $_) } }
  9         18  
  8         9  
  8         13  
  8         13  
29 1         6 else { while () { chomp; run_command(\%o, $_) } }
  2         21  
  2         4  
30              
31 9         70 return;
32             }
33              
34             # Run the command and display the output, echoing if so configured
35             sub run_command {
36 10     10   20 my $o = shift;
37 10 100       68 print "> $_[0]\n" if $o->{echo};
38              
39 10         22 my $resp = $o->{rcon}->command($_[0]);
40 10 100       211 my $disp = $o->{color} ? $resp->ansi : $resp->plain;
41              
42 10 100       79 print length $resp ? "$disp\n" : "[Command sent]\n" unless $o->{quiet};
    100          
43             }
44              
45             __END__