blib/lib/Class/Handle.pm

filter: Statementstmt 77.6% 52 / 67 Branchbran 44.4% 24 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.7% 21 / 26 Podpod 94.7% 18 / 19 totaltotal 69.2% 115 / 166 CC 28 SCAR 20.0
CC
28
Cov
63%
load
42.0
methods
6.0
super_path
6.0
CRAP 7.4
↑ Summary
1 package Class::Handle;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Class::Handle - Create objects that are handles to Classes
8  
9 =head1 SYNOPSIS
10  
11 # Create a class handle
12 use Class::Handle;
13 my $class = Class::Handle->new( 'Foo::Class' );
14 my $name = $class->name;
15
16 # UNIVERSAL type methods
17 $class->VERSION();
18 $class->isa( 'Foo:Bar' );
19 $class->can( 'blah' );
20
21 # Class::Inspector type methods
22 $class->installed();
23 $class->loaded();
24 $class->filename();
25 $class->resolved_filename();
26 $class->functions();
27 $class->function_refs();
28 $class->function_exists( 'function' );
29 $class->methods( 'public', 'full' );
30 $class->subclasses();
31
32 # Class::ISA type methods
33 $class->super_path();
34 $class->self_and_super_path();
35 $class->full_super_path();
36
37 # Loading and unloading
38 $class->load();
39  
40 =head1 DESCRIPTION
41  
42 Class related functionality in Perl is broken up into a variety of different
43 modules. Class::Handle attempts to provide a convenient object wrapper around
44 the various different types of functions that can be performed on a class.
45  
46 Please note that this is an initial non-production quality release, and should
47 be used as such. Functionality and API are subject to change without notice.
48  
49 Currently, Class::Handle provies what is effectively a combined API from
50 C<UNIVERSAL>, C<Class::ISA> and C<Class::Inspector> for obtaining information
51 about a Class, and some additional task methods, such as C<load> to common
52 tasks relating to classes.
53  
54 =head1 UNIVERSAL API
55  
56 To ensure we maintain compliance with other classes that rely on
57 methods provided by C<UNIVERSAL>, Class::Handle acts in the normal way when
58 something like C<<Class::Handle->VERSION>> is called. That is, it returns the
59 version of Class::Handle itself. When C<UNIVERSAL> methods are called on
60 an instantiation the method is changed to act on the class we have a handle
61 to. For example, the two following statements are equivalent.
62  
63 # Getting the version directly
64 print Foo::Bar->VERSION;
65
66 # Getting the version via Class::Handle
67 my $class = Class::Handle->new( 'Foo::Bar' );
68 print $class->VERSION;
69  
70 This also applies to the C<isa> and C<can> methods.
71  
72 =head1 METHODS
73  
74 =cut
75  
76 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use UNIVERSAL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
79 2 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
80 2 ▶use Class::Inspector ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 # Set the version
83 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
84 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
85 2 $VERSION = '1.07';
86 }
87  
88  
89  
90  
91  
92 #####################################################################
93 # Constructor
94  
95 =pod
96  
97 =head2 new $class
98  
99 The C<new> constructor will create a new handle to a class or unknown
100 existance or status. That is, it won't check that the class actually exists
101 at this time. It WILL however check to make sure that your class name is legal.
102  
103 Returns a new Class::Handle object on success
104 Returns undef if the class name is illegal
105  
106 =cut
107  
108 12 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
109 12 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 12 12
110  
111 # Get and check the class name
112 12 ▶ my $name = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or return undef 11 1 12
113 11 ▶ $name = 'main' if $name eq '::';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "::" 1 10 11
114 11 $name =~ s/^::/main::/;
115 11 ▶ return undef unless $name =~ /^[a-z]\w*((?:'|::)\w+)*$/io;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[a-z]\w*((?:'|::)\w+)*$/io 2 9 11
116  
117 # Create and return the object
118 9 bless { name => $name }, $class;
119 }
120  
121 =pod
122  
123 =head2 name
124  
125 The c<name> method returns the name of the class as original specified in
126 the constructor.
127  
128 =cut
129  
130 1 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
131  
132  
133  
134  
135  
136 #####################################################################
137 # UNIVERSAL Methods
138  
139 =pod
140  
141 =head2 VERSION
142  
143 Find the version for the class. Does not check that the class is loaded ( at
144 this time ).
145  
146 Returns the version on success, C<undef> if the class does not defined a
147 C<$VERSION> or the class is not loaded.
148  
149 =cut
150  
151 2 ▶sub VERSION {
Subroutine VERSION: called CC=2 SCAR=6.9
Pod documented
152 2 my $either = shift;
153  
154 # In the special case that someone wants to know OUR version,
155 # let them find it out as normal. Otherwise, return the VERSION
156 # for the class we point to.
157 ref $either
158 ? UNIVERSAL::VERSION( $either->{name} )
159 2 ▶ : UNIVERSAL::VERSION( $either );
Branch: 1 branch
BranchTrueFalseTotal
ref $either ? : 1 1 2
160 }
161  
162 =pod
163  
164 =head2 isa $class
165  
166 Checks to see if the class is a subclass of another class. Does not check that
167 the class is loaded ( at this time ).
168  
169 Returns true/false as for C<UNIVERSAL::isa>
170  
171 =cut
172  
173 6 ▶sub isa {
Subroutine isa: called CC=3 SCAR=11.1
Pod documented
174 6 my $either = shift;
175 6 ▶ my $isa = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $isa = shift() or return undef 6 0 6
176  
177 # In the special case that someone wants to know an isa for
178 # OUR version, let them find it out as normal. Otherwise, return
179 # the isa for the class we point to.
180 ref $either
181 6 ▶ ? UNIVERSAL::isa( $either->{name}, $isa )
Branch: 1 branch
BranchTrueFalseTotal
ref $either ? : 3 3 6
182 : UNIVERSAL::isa( $either, $isa );
183 }
184  
185 =pod
186  
187 =head2 can $method
188  
189 Checks to see if a particular method is defined for the class.
190  
191 Returns a C<CODE> ref to the function is the method is available,
192 or false if the class does not have that method available.
193  
194 =cut
195  
196 2 ▶sub can {
Subroutine can: called CC=3 SCAR=11.1
Pod documented
197 2 my $either = shift;
198 2 ▶ my $can = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $can = shift() or return undef 2 0 2
199  
200 # In the special case that someone wants to know a "cab" for
201 # OUR versoin, let them find it out as normal. Otherwise, return
202 # the can for the class we point to.
203 ref $either
204 2 ▶ ? UNIVERSAL::can( $either->{name}, $can )
Branch: 1 branch
BranchTrueFalseTotal
ref $either ? : 1 1 2
205 : UNIVERSAL::can( $either, $can );
206 }
207  
208  
209  
210  
211  
212 #####################################################################
213 # Class::Inspector methods
214  
215 =pod
216  
217 =head2 installed
218  
219 Checks to see if a particular class is installed on the machine, or at least
220 that the class is available to perl. In this case, "class" really means
221 "module". This methods cannot detect a class that is not a module. ( Has its
222 own file ).
223  
224 Returns true if the class is installed and available, or false otherwise.
225  
226 =cut
227  
228 2 ▶sub installed {
Subroutine installed: called CC=2 SCAR=7.2
Pod documented
229 2 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
230 2 Class::Inspector->installed( $self->{name} );
231 }
232  
233 =pod
234  
235 =head2 loaded
236  
237 Checks to see if a class is loaded. In this case, "class" does NOT mean
238 "module". The C<loaded> method will return true for classes that do not have
239 their own file.
240  
241 For example, if a module C<Foo> contains the classes C<Foo>, C<Foo::Bar> and
242 C<Foo::Buffy>, the C<loaded> method will return true for all of the classes.
243  
244 Returns true if the class is loaded, or false otherwise.
245  
246 =cut
247  
248 2 ▶sub loaded {
Subroutine loaded: called CC=2 SCAR=7.2
Pod documented
249 2 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
250 2 Class::Inspector->loaded( $self->{name} );
251 }
252  
253 =pod
254  
255 =head2 filename
256  
257 Returns the base filename for a class. For example, for the class
258 C<Foo::Bar>, C<loaded> would return C<"Foo/Bar.pm">.
259  
260 The C<filename> method is platform neutral, it should always return the
261 filename in the correct format for your platform.
262  
263 =cut
264  
265 1 ▶sub filename {
Subroutine filename: called CC=2 SCAR=7.2
Pod documented
266 1 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
267 1 Class::Inspector->filename( $self->{name} );
268 }
269  
270 =pod
271  
272 =head2 resolved_filename @extra_paths
273  
274 The C<resolved_filename> will attempt to find the real file on your system
275 that will be used when a class is loaded. If additional paths are provided
276 as argument, they will be tried first, before the contents of the @INC array.
277 If a file cannot be found to match the class, returns false.
278  
279 =cut
280  
281 1 ▶sub resolved_filename {
Subroutine resolved_filename: called CC=2 SCAR=7.2
Pod documented
282 1 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
283 1 Class::Inspector->resolved_filename( $self->{name} );
284 }
285  
286 =pod
287  
288 =head2 loaded_filename
289  
290 If the class is loaded, returns the name of the file that it was originally
291 loaded from.
292  
293 Returns false if the class is not loaded, or did not have its own file.
294  
295 =cut
296  
297 1 ▶sub loaded_filename {
Subroutine loaded_filename: called CC=2 SCAR=7.2
Pod documented
298 1 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
299 1 Class::Inspector->loaded_filename( $self->{name} );
300 }
301  
302 =pod
303  
304 =head2 functions
305  
306 Returns a list of the names of all the functions in the classes immediate
307 namespace. Note that this is not the METHODS of the class, just the functions.
308 Returns a reference to an array of the function names on success.
309  
310 Returns undef on error or if the class is not loaded.
311  
312 =cut
313  
314 2 ▶sub functions {
Subroutine functions: called CC=2 SCAR=7.2
Pod documented
315 2 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
316 2 Class::Inspector->functions( $self->{name} );
317 }
318  
319 =pod
320  
321 =head2 function_refs
322  
323 Returns a list of references to all the functions in the classes immediate
324 namespace.
325  
326 Returns a reference to an array of CODE refs of the functions on
327 success, or C<undef> on error or if the class is not loaded.
328  
329 =cut
330  
331 2 ▶sub function_refs {
Subroutine function_refs: called CC=2 SCAR=7.2
Pod documented
332 2 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
333 2 Class::Inspector->function_refs( $self->{name} );
334 }
335  
336 =pod
337  
338 =head2 function_exists $function
339  
340 Checks to see if the function exists in the class. Note that this is as a
341 function, not as a method. To see if a method exists for a class, use the
342 C<can> method in UNIVERSAL, and hence to every other class.
343  
344 Returns true if the function exists, false if the function does not exist,
345 or C<undef> on error, or if the class is not loaded.
346  
347 =cut
348  
349 3 ▶sub function_exists {
Subroutine function_exists: called CC=2 SCAR=7.2
Pod documented
350 3 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 0 3
351 3 Class::Inspector->function_exists( $self->{name}, @_ );
352 }
353  
354 =pod
355  
356 =head2 methods @options
357  
358 Attempts to find the methods available to the class. This includes everything
359 in the classes super path up to, but NOT including, UNIVERSAL. Returns a
360 reference to an array of the names of all the available methods on success.
361 Returns undef if the class is not loaded.
362  
363 Any provided options are passed through, and alter the response in the same
364 way as for the options to C<<Class::Inspector->methods()>>, that is, 'public',
365 'private', 'full' and 'expanded', and combinations thereof.
366  
367 =cut
368  
369 0 ▶sub methods {
Subroutine methods: not called CC=2 SCAR=17.9
Pod documented
370 0 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
371 0 Class::Inspector->methods( $self->{name}, @_ );
372 }
373  
374 =pod
375  
376 =head2 subclasses
377  
378 The C<subclasses> method will search then entire namespace (and thus
379 B<all> currently loaded classes) to find all of the subclasses of the
380 class handle.
381  
382 The actual test will be done by calling C<isa> on the class as a static
383 method. (i.e. C<<My::Class->isa($class)>>.
384  
385 Returns a reference to a list of the names of the loaded classes that match
386 the class provided, or false is none match, or C<undef> if the class name
387 provided is invalid.
388  
389 =cut
390  
391 1 ▶sub subclasses {
Subroutine subclasses: called CC=2 SCAR=7.2
Pod documented
392 1 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
393 1 Class::Inspector->subclasses( $self->{name}, @_ );
394 }
395  
396  
397  
398  
399  
400 #####################################################################
401 # Class::ISA Methods
402  
403 =pod
404  
405 =head2 super_path
406  
407 The C<super_path> method is a straight pass through to the
408 C<Class::ISA::super_path> function. Returns an ordered list of
409 class names, with no duplicates. The list does NOT include the class itself,
410 or the L<UNIVERSAL> class.
411  
412 =cut
413  
414 0 ▶sub super_path {
Subroutine super_path: not called CC=2 SCAR=17.9
Pod documented
415 0 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
416 0 Class::ISA::super_path( $self->{name} );
417 }
418  
419 =pod
420  
421 =head2 self_and_super_path
422  
423 As above, but includes ourself at the beginning of the path. Directly
424 passes through to L<Class::ISA>.
425  
426 =cut
427  
428 0 ▶sub self_and_super_path {
Subroutine self_and_super_path: not called CC=2 SCAR=17.9
Pod documented
429 0 ▶ my $self = ref $_[0] ? shift : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
430 0 Class::ISA::self_and_super_path( $self->{name} );
431 }
432  
433 =pod
434  
435 =head2 full_super_path
436  
437 The C<full_super_path> method is an additional method not in C<Class::ISA>.
438 It returns as for C<super_path>, except that it also contains BOTH the
439 class itself, and C<UNIVERSAL>. This full list is more technically accurate,
440 but less commonly used, and as such isn't available from L<Class::ISA>
441 itself.
442  
443 =cut
444  
445 0 ▶sub full_super_path {
Subroutine full_super_path: not called CC=2 SCAR=17.9
Pod documented
446 0 ▶ my $self = ref $_[0] ? shift : return ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
447 0 Class::ISA::self_and_super_path( $self->{name} ), 'UNIVERSAL';
448 }
449  
450  
451  
452  
453  
454  
455 #####################################################################
456 # Task Methods
457  
458 # These methods are specific to Class::Handle and provide simpler
459 # interfaces to common tasks.
460  
461 # Run-time load a class, as if it were a C<use>, including import.
462 # Roughly equivalent to require $name; $name->import;
463 0 ▶sub load {
Subroutine load: not called CC=6 SCAR=37.4
Pod undocumented
464 0 ▶ my $self = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or return undef 0 0 0
465  
466 # Shortcut if the class is already loaded
467 0 ▶ return 1 if Class::Inspector->loaded( $self->{name} );
Branch: 1 branch
BranchTrueFalseTotal
if "Class::Inspector"->loaded($self->{"name"}) 0 0 0
468  
469 # Get the resolved file name
470 0 ▶ my $filename = $self->resolved_filename() or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $self->resolved_filename or return undef 0 0 0
471  
472 # Load the class
473 0 ▶ require $filename or return undef;
Branch: 1 branch
BranchTrueFalseTotal
require $filename or return undef 0 0 0
474  
475 # Do we need to call an import method?
476 0 ▶ my $import = $self->can( 'import' ) or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $import = $self->can("import") or return 1 0 0 0
477  
478 # Go to the import
479 0 goto &{$import};
480 }
481  
482 2 1;
483  
484 =pod
485  
486 =head1 BUGS
487  
488 No known bugs. Additional feature requests are being taken.
489  
490 =head1 SUPPORT
491  
492 Bugs should be reported via the CPAN bug tracking system
493  
494 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Class-Handle>
495  
496 For other inquiries, contact the author
497  
498 =head1 AUTHOR
499  
500 Adam Kennedy E<lt>adamk@cpan.orgE<gt>, L<http://ali.as/>
501  
502 =head1 SEE ALSO
503  
504 C<UNIVERSAL>, C<Class::ISA>, and C<Class::Inspector>, which provide
505 most of the functionality for this class.
506  
507 =head1 COPYRIGHT
508  
509 Copyright (c) 2002 - 2006 Adam Kennedy.
510  
511 This program is free software; you can redistribute
512 it and/or modify it under the same terms as Perl itself.
513  
514 The full text of the license can be found in the
515 LICENSE file included with this module.
516  
517 =cut
↑ Summary