File Coverage

blib/lib/Process/Probe.pm
Criterion Covered Total %
statement 59 63 93.6
branch 9 14 64.2
condition 0 3 0.0
subroutine 14 14 100.0
pod 2 5 40.0
total 84 99 84.8


line stmt bran cond sub pod time code
1             package Process::Probe;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Process::Probe - Process to test if any named classes are installed
8              
9             =head1 SYNOPSIS
10              
11             my $probe = Process::Probe->new( qw{
12             My::Process
13             CPAN::Module::Process
14             Something::Else
15             } );
16            
17             $probe->run;
18            
19             # Lists of classes
20             my @yep = $probe->available;
21             my @nope = $probe->unavailable;
22             my @maybe = $probe->unknown;
23            
24             # Test for single class with any of the above
25             if ( $probe->available('My::Process') ) {
26             print "My::Process is available\n";
27             }
28              
29             =head1 DESCRIPTION
30              
31             B is a simple and standardised class available that is
32             available with the core L distribution. It is used to probe
33             a host to determine whether or not the remote host has certain process
34             classes installed.
35              
36             By default, the object will search through the system's include path to
37             find the .pm files that match the particular classes.
38              
39             Typical examples of using the default functionality could include
40             executing a B object via a SSH login on a remote host
41             to determine which of a set of desired classes exist on the remote host.
42              
43             The probe will ONLY check for the existance of classes that are in the
44             unknown state at the time the C method is called.
45              
46             In scenarios where the requestor does not have direct execution rights
47             on the remote host, and the request is being marshalled via a server
48             process, this allows security code on the server to preset forbidden
49             classes to no before the probe is run, or to otherwise manipulate the
50             "answer" to the "question" that B represents.
51              
52             No functionality is provided to query ALL the C-compatible
53             classes on a remote host. This is intentional. It prevents very
54             disk-intensive scans, protects remote host against hostile requests,
55             and prevents the use of these objects en-mass as a denial of service.
56              
57             =cut
58              
59 2     2   26272 use 5.00503;
  2         7  
  2         74  
60 2     2   12 use strict;
  2         3  
  2         56  
61 2     2   11 use File::Spec ();
  2         4  
  2         25  
62 2     2   10 use List::Util ();
  2         4  
  2         31  
63 2     2   969 use Params::Util ();
  2         6461  
  2         35  
64 2     2   698 use Process::Delegatable ();
  2         5  
  2         29  
65 2     2   514 use Process ();
  2         6  
  2         41  
66              
67 2     2   8 use vars qw{$VERSION @ISA};
  2         3  
  2         117  
68             BEGIN {
69 2     2   5 $VERSION = '0.30';
70 2         925 @ISA = qw{
71             Process::Delegatable
72             Process
73             };
74             }
75              
76             sub new {
77 2     2 1 744 my $class = shift;
78              
79             # Create the object
80 2         8 my $self = bless {
81             modules => { },
82             }, $class;
83              
84             # Add the modules to test for
85 2         8 while ( @_ ) {
86 6         9 my $module = shift;
87 6 50       282 unless ( Params::Util::_CLASS($module) ) {
88 0         0 return undef;
89             }
90 6         106 $self->{modules}->{$module} = undef;
91             }
92              
93 2         6 return $self;
94             }
95              
96             sub run {
97 1     1 1 3 my $self = shift;
98 1         4 my $hash = $self->{modules};
99 1         4 foreach my $key ( sort keys %$hash ) {
100 3         5 $hash->{$key} = 0;
101 3         11 my @path = split /::/, $key;
102 3         7 foreach my $dir ( @INC ) {
103 26 50       52 next if ref $dir;
104 26 100       885 next unless -f File::Spec->catfile($dir, @path) . '.pm';
105 2         6 $hash->{$key} = 1;
106 2         7 last;
107             }
108             }
109 1         3 return 1;
110             }
111              
112             sub available {
113 3     3 0 8699 my $self = shift;
114 3         9 my $hash = $self->{modules};
115 3 50       11 if ( @_ ) {
116             return !! (
117 0         0 $hash->{$_[0]}
118             );
119             } else {
120 9         50 return grep {
121 3         27 $hash->{$_}
122             } sort keys %$hash;
123             }
124             }
125              
126             sub unavailable {
127 3     3 0 8 my $self = shift;
128 3         13 my $hash = $self->{modules};
129 3 50       10 if ( @_ ) {
130             return !! (
131 0   0     0 defined $hash->{$_[0]}
132             and
133             not $hash->{$_[0]}
134             );
135             } else {
136 9 100       61 return grep {
137 3         13 defined $hash->{$_} and not $hash->{$_}
138             } sort keys %$hash;
139             }
140             }
141              
142             sub unknown {
143 3     3 0 6 my $self = shift;
144 3         7 my $hash = $self->{modules};
145 3 50       11 if ( @_ ) {
146             return !! (
147 0         0 not defined $hash->{$_[0]}
148             );
149             } else {
150 9         33 return grep {
151 3         15 not defined $hash->{$_}
152             } sort keys %$hash;
153             }
154             }
155              
156             1;
157              
158             =pod
159              
160             =head1 SUPPORT
161              
162             Bugs should be reported via the CPAN bug tracker at
163              
164             L
165              
166             For other issues, contact the author.
167              
168             =head1 AUTHOR
169              
170             Adam Kennedy Eadamk@cpan.orgE
171              
172             =head1 SEE ALSO
173              
174             L
175              
176             =head1 COPYRIGHT
177              
178             Copyright 2006 - 2011 Adam Kennedy.
179              
180             This program is free software; you can redistribute
181             it and/or modify it under the same terms as Perl itself.
182              
183             The full text of the license can be found in the
184             LICENSE file included with this module.
185              
186             =cut