From: Robert 'phaylon' Sedlacek Date: Wed, 9 May 2012 22:29:46 +0000 (+0000) Subject: use command pipe output and exception handling utils, put process list under 'process... X-Git-Tag: v0.001_001~98 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fd6e42bf5619ea0485743b68e911176167148632;p=scpubgit%2FSystem-Introspector.git use command pipe output and exception handling utils, put process list under 'processes' to separate from 'error' --- diff --git a/lib/System/Introspector/Processes.pm b/lib/System/Introspector/Processes.pm index a4a67b1..9fb0726 100644 --- a/lib/System/Introspector/Processes.pm +++ b/lib/System/Introspector/Processes.pm @@ -1,23 +1,10 @@ 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( @@ -54,12 +41,32 @@ 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;