| 1 | package Proc::ProcessTable::Match::State; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Proc::ProcessTable::Match::State - Check if the state of a process matches via regexp. | ||||||||||
| 10 | |||||||||||
| 11 | =head1 VERSION | ||||||||||
| 12 | |||||||||||
| 13 | Version 0.1.0 | ||||||||||
| 14 | |||||||||||
| 15 | =cut | ||||||||||
| 16 | |||||||||||
| 17 | 2 | our $VERSION = '0.1.0'; | |||||||||
| 18 | |||||||||||
| 19 | |||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||
| 21 | |||||||||||
| 22 | use Proc::ProcessTable::Match::State; | ||||||||||
| 23 | |||||||||||
| 24 | my %args=( | ||||||||||
| 25 | states=>[ | ||||||||||
| 26 | 'zombie', | ||||||||||
| 27 | 'wait', | ||||||||||
| 28 | ], | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | my $checker=Proc::ProcessTable::Match::State->new( \%args ); | ||||||||||
| 32 | |||||||||||
| 33 | if ( $checker->match( $proc ) ){ | ||||||||||
| 34 | print "It matches.\n"; | ||||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | =head1 METHODS | ||||||||||
| 38 | |||||||||||
| 39 | =head2 new | ||||||||||
| 40 | |||||||||||
| 41 | This intiates the object. | ||||||||||
| 42 | |||||||||||
| 43 | It takes a hash reference with one key. One key is required and | ||||||||||
| 44 | that is 'states', which is a array of states to match. | ||||||||||
| 45 | |||||||||||
| 46 | The matching is done via regexp. | ||||||||||
| 47 | |||||||||||
| 48 | Atleast one value must be specified. | ||||||||||
| 49 | |||||||||||
| 50 | If the new method fails, it dies. | ||||||||||
| 51 | |||||||||||
| 52 | my %args=( | ||||||||||
| 53 | states=>[ | ||||||||||
| 54 | 'wait', | ||||||||||
| 55 | 'zombie', | ||||||||||
| 56 | ], | ||||||||||
| 57 | ); | ||||||||||
| 58 | |||||||||||
| 59 | my $checker=Proc::ProcessTable::Match::State->new( \%args ); | ||||||||||
| 60 | |||||||||||
| 61 | =cut | ||||||||||
| 62 | |||||||||||
| 63 | 8 | ▶ | sub new{ | ||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||
| 64 | 8 | my %args; | |||||||||
| 65 | 8 | ▶ | if(defined($_[1])){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 7 | %args= %{$_[1]}; | |||||||||
| 67 | }; | ||||||||||
| 68 | |||||||||||
| 69 | # run some basic checks to make sure we have the minimum stuff required to work | ||||||||||
| 70 | 8 | ▶ | if ( ! defined( $args{states} ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 2 | die ('No states key specified in the argument hash'); | |||||||||
| 72 | } | ||||||||||
| 73 | 6 | ▶ | if ( ref( $args{states} ) ne 'ARRAY' ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 1 | die ('The states key is not a array'); | |||||||||
| 75 | } | ||||||||||
| 76 | 5 | ▶ | if ( ! defined $args{states}[0] ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 1 | die ('Nothing defined in the states array'); | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | # make sure each specified value compiles as a regex | ||||||||||
| 81 | 4 | my $regex_int=0; | |||||||||
| 82 | 4 | ▶ | while ( defined( $args{states}[$regex_int] ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 5 | ▶ | if ( ! eval{ qr/$args{states}[$regex_int]/ } ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 1 | die (q{The value "}.$args{states}[$regex_int].q{" in the states array does not compile as a regex... }.$@); | |||||||||
| 85 | } | ||||||||||
| 86 | 4 | $regex_int++; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 3 | my $class=$_[0]; | |||||||||
| 90 | my $self = { | ||||||||||
| 91 | states=>$args{states}, | ||||||||||
| 92 | 3 | }; | |||||||||
| 93 | 3 | bless $self, $class; | |||||||||
| 94 | |||||||||||
| 95 | 3 | return $self; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | =head2 match | ||||||||||
| 99 | |||||||||||
| 100 | Checks if a single Proc::ProcessTable::Process object matches the stack. | ||||||||||
| 101 | |||||||||||
| 102 | One argument is taken and that is a Proc::ProcessTable::Process object. | ||||||||||
| 103 | |||||||||||
| 104 | The returned value is a boolean. | ||||||||||
| 105 | |||||||||||
| 106 | if ( $checker->match( $proc ) ){ | ||||||||||
| 107 | print "The process matches.\n"; | ||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | =cut | ||||||||||
| 111 | |||||||||||
| 112 | 6 | ▶ | sub match{ | ||||||||
|
Subroutine
match: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 113 | 6 | my $self=$_[0]; | |||||||||
| 114 | 6 | my $object=$_[1]; | |||||||||
| 115 | |||||||||||
| 116 | 6 | ▶ | if ( !defined( $object ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 1 | return 0; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | 5 | ▶ | if ( ref( $object ) ne 'Proc::ProcessTable::Process' ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 1 | return 0; | |||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | 4 | my $proc_state; | |||||||||
| 125 | 4 | eval{ | |||||||||
| 126 | 4 | $proc_state=$object->state; | |||||||||
| 127 | }; | ||||||||||
| 128 | |||||||||||
| 129 | # don't bother proceeding, the object won't match ever | ||||||||||
| 130 | # as it does not have a state | ||||||||||
| 131 | 4 | ▶ | if ( ! defined( $proc_state ) ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 1 | return 0; | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | 3 | foreach my $state ( @{ $self->{states} } ){ | |||||||||
| 136 | 4 | ▶ | if ( $proc_state =~ /$state/ ){ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 2 | return 1; | |||||||||
| 138 | } | ||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 1 | return 0; | |||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | =head1 AUTHOR | ||||||||||
| 145 | |||||||||||
| 146 | Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >> | ||||||||||
| 147 | |||||||||||
| 148 | =head1 BUGS | ||||||||||
| 149 | |||||||||||
| 150 | Please report any bugs or feature requests to C<bug-Proc-ProcessTable-Match at rt.cpan.org>, or through | ||||||||||
| 151 | the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Proc-ProcessTable-Match>. I will be notified, and then you'll | ||||||||||
| 152 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 153 | |||||||||||
| 154 | |||||||||||
| 155 | |||||||||||
| 156 | |||||||||||
| 157 | =head1 SUPPORT | ||||||||||
| 158 | |||||||||||
| 159 | You can find documentation for this module with the perldoc command. | ||||||||||
| 160 | |||||||||||
| 161 | perldoc Proc::ProcessTable::Match | ||||||||||
| 162 | |||||||||||
| 163 | |||||||||||
| 164 | You can also look for information at: | ||||||||||
| 165 | |||||||||||
| 166 | =over 4 | ||||||||||
| 167 | |||||||||||
| 168 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||
| 169 | |||||||||||
| 170 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Proc-ProcessTable-Match> | ||||||||||
| 171 | |||||||||||
| 172 | =item * Search CPAN | ||||||||||
| 173 | |||||||||||
| 174 | L<https://metacpan.org/release/Proc-ProcessTable-Match> | ||||||||||
| 175 | |||||||||||
| 176 | =back | ||||||||||
| 177 | |||||||||||
| 178 | |||||||||||
| 179 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 180 | |||||||||||
| 181 | |||||||||||
| 182 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 183 | |||||||||||
| 184 | Copyright 2019 Zane C. Bowers-Hadley. | ||||||||||
| 185 | |||||||||||
| 186 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 187 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 188 | copy of the full license at: | ||||||||||
| 189 | |||||||||||
| 190 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 191 | |||||||||||
| 192 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 193 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 194 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 195 | or distribute the Package, if you do not accept this license. | ||||||||||
| 196 | |||||||||||
| 197 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 198 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 199 | your Modified Version complies with the requirements of this license. | ||||||||||
| 200 | |||||||||||
| 201 | This license does not grant you the right to use any trademark, service | ||||||||||
| 202 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 203 | |||||||||||
| 204 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 205 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 206 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 207 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 208 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 209 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 210 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 211 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 212 | |||||||||||
| 213 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 214 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 215 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 216 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 217 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 218 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 219 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 220 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||
| 221 | |||||||||||
| 222 | |||||||||||
| 223 | =cut | ||||||||||
| 224 | |||||||||||
| 225 | 2 | 1; # End of Proc::ProcessTable::Match | |||||||||