ProcessTable/Role/Table/PPT.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 89 CC 4 SCAR 14.4
CC
4
Cov
0%
_convert_process
12.0
_process_hash
6.0
table
2.0
CRAP 4.2
1 package P9Y::ProcessTable::Role::Table::PPT;
2  
3 our $AUTHORITY = 'cpan:BBYRD'; # AUTHORITY
4 our $VERSION = '1.06_01'; # VERSION
5  
6 #############################################################################
7 # Modules
8  
9 # use sanity;
10 use strict qw(subs vars);
11 no strict 'refs';
12 use warnings FATAL => 'all';
13 no warnings qw(uninitialized);
14  
15 use Moo::Role;
16  
17 requires 'process';
18  
19 use Proc::ProcessTable 0.48; # ie: the one that ain't broke
20 use List::AllUtils 'first';
21  
22 use namespace::clean;
23 no warnings 'uninitialized';
24  
25 my $pt = Proc::ProcessTable->new();
26  
27 #############################################################################
28 # Methods
29  
30 # Unfortunately, P:PT has no concept of anything except "grab everything at once". So, we need to run
31 # through these wasteful cycles just to get one process, one list of PIDs, etc.
32  
33 sub table {
34 my $self = shift;
35 return map {
36 my $hash = $self->_convert_process($_);
37 $hash->{_pt_obj} = $self;
38 P9Y::ProcessTable::Process->new($hash);
39 } @{ $pt->table };
40 }
41  
42 sub list {
43 my $self = shift;
44 return sort { $a <=> $b } map { $_->pid } @{ $pt->table };
45 }
46  
47 sub fields {
48 # sometimes an undef pops through
49 return grep { $_ } $pt->fields;
50 }
51  
52 sub _process_hash {
53 my ($self, $pid) = @_;
54 my $process = first { $_->pid == $pid } @{ $pt->table };
55 return unless $process;
56 return $self->_convert_process($process);
57 }
58  
59 sub _convert_process {
60 my ($self, $process) = @_;
61 return unless $process;
62  
63 my $hash = {};
64 # (only has the ones that are different)
65 my $stat_loc = { qw/
66 cmdline cmndline
67 / };
68  
69 foreach my $key ( $self->fields ) {
70 my $old = $stat_loc->{$key} || $key;
71 my $item = eval { $process->$old() };
72  
73 $hash->{$key} = $item if defined $item;
74 }
75  
76 return $hash;
77 }
78  
79 42;