package System::Introspector::Processes;
use Moo;
-sub gather {
- my ($self) = @_;
- my $pipe = $self->_open_ps_pipe;
- my $spec = <$pipe>;
- $spec =~ s{(?:^\s+|\s+$)}{}g;
- my @fields = map lc, split m{\s+}, $spec;
- my @rows;
- while (defined( my $line = <$pipe> )) {
- chomp $line;
- $line =~ s{(?:^\s+|\s+$)}{}g;
- my @values = split m{\s+}, $line, scalar @fields;
- my %row;
- @row{ @fields } = @values;
- push @rows, \%row;
- }
- return \@rows;
-}
+use System::Introspector::Util qw(
+ handle_from_command
+ transform_exceptions
+);
# args is automatically included, since it has to be last
my @Included = qw(
wchan
);
+sub gather {
+ my ($self) = @_;
+ my @names = (@Included, 'args');
+ return transform_exceptions {
+ my $pipe = $self->_open_ps_pipe;
+ my $spec = <$pipe>;
+ $spec =~ s{(?:^\s+|\s+$)}{}g;
+ my @fields = map lc, split m{\s+}, $spec;
+ my @rows;
+ while (defined( my $line = <$pipe> )) {
+ chomp $line;
+ $line =~ s{(?:^\s+|\s+$)}{}g;
+ my @values = split m{\s+}, $line, scalar @fields;
+ my %row;
+ @row{ @names } = @values;
+ push @rows, \%row;
+ }
+ return { processes => \@rows };
+ };
+}
+
sub _open_ps_pipe {
my ($self) = @_;
- my $command = sprintf 'ps -eo %s', join(',', @Included, 'args');
- open my $pipe, '-|', $command
- or die "Unable to open pipe to '$command': $!\n";
- return $pipe;
+ return handle_from_command sprintf
+ 'ps -eo %s',
+ join(',', @Included, 'args');
}
1;