File Coverage

blib/lib/Class/Handle.pm
Criterion Covered Total %
statement 52 67 77.6
branch 24 54 44.4
condition n/a
subroutine 21 26 80.7
pod 18 19 94.7
total 115 166 69.2


line stmt bran cond sub pod time code
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, C and C for obtaining information
51             about a Class, and some additional task methods, such as C 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, Class::Handle acts in the normal way when
58             something like C<VERSION>> is called. That is, it returns the
59             version of Class::Handle itself. When C 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 and C methods.
71              
72             =head1 METHODS
73              
74             =cut
75              
76 2     2   2511 use 5.005;
  2         6  
  2         71  
77 2     2   10 use strict;
  2         3  
  2         59  
78 2     2   8539 use UNIVERSAL ();
  2         28  
  2         107  
79 2     2   1894 use Class::ISA ();
  2         6810  
  2         36  
80 2     2   1805 use Class::Inspector ();
  2         7755  
  2         112  
81              
82             # Set the version
83 2     2   16 use vars qw{$VERSION};
  2         3  
  2         130  
84             BEGIN {
85 2     2   2018 $VERSION = '1.07';
86             }
87              
88              
89              
90              
91              
92             #####################################################################
93             # Constructor
94              
95             =pod
96              
97             =head2 new $class
98              
99             The C 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             sub new {
109 12 50   12 1 501 my $class = ref $_[0] ? ref shift : shift;
110              
111             # Get and check the class name
112 12 100       36 my $name = shift or return undef;
113 11 100       31 $name = 'main' if $name eq '::';
114 11         22 $name =~ s/^::/main::/;
115 11 100       79 return undef unless $name =~ /^[a-z]\w*((?:'|::)\w+)*$/io;
116              
117             # Create and return the object
118 9         56 bless { name => $name }, $class;
119             }
120              
121             =pod
122              
123             =head2 name
124              
125             The c method returns the name of the class as original specified in
126             the constructor.
127              
128             =cut
129              
130 1     1 1 2000 sub name { $_[0]->{name} }
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 if the class does not defined a
147             C<$VERSION> or the class is not loaded.
148              
149             =cut
150              
151             sub VERSION {
152 2     2 1 5 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 2 100       32 ref $either
158             ? UNIVERSAL::VERSION( $either->{name} )
159             : UNIVERSAL::VERSION( $either );
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
170              
171             =cut
172              
173             sub isa {
174 6     6 1 6145 my $either = shift;
175 6 50       21 my $isa = shift or return undef;
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 6 100       50 ref $either
181             ? UNIVERSAL::isa( $either->{name}, $isa )
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 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             sub can {
197 2     2 1 7 my $either = shift;
198 2 50       7 my $can = shift or return undef;
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 2 100       17 ref $either
204             ? UNIVERSAL::can( $either->{name}, $can )
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             sub installed {
229 2 50   2 1 1221 my $self = ref $_[0] ? shift : return undef;
230 2         10 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 method will return true for classes that do not have
239             their own file.
240              
241             For example, if a module C contains the classes C, C and
242             C, the C 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             sub loaded {
249 2 50   2 1 476 my $self = ref $_[0] ? shift : return undef;
250 2         12 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, C would return C<"Foo/Bar.pm">.
259              
260             The C method is platform neutral, it should always return the
261             filename in the correct format for your platform.
262              
263             =cut
264              
265             sub filename {
266 1 50   1 1 588 my $self = ref $_[0] ? shift : return undef;
267 1         8 Class::Inspector->filename( $self->{name} );
268             }
269              
270             =pod
271              
272             =head2 resolved_filename @extra_paths
273              
274             The C 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             sub resolved_filename {
282 1 50   1 1 613 my $self = ref $_[0] ? shift : return undef;
283 1         29 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             sub loaded_filename {
298 1 50   1 1 640 my $self = ref $_[0] ? shift : return undef;
299 1         7 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             sub functions {
315 2 50   2 1 1574 my $self = ref $_[0] ? shift : return undef;
316 2         10 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 on error or if the class is not loaded.
328              
329             =cut
330              
331             sub function_refs {
332 2 50   2 1 1245 my $self = ref $_[0] ? shift : return undef;
333 2         9 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 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 on error, or if the class is not loaded.
346              
347             =cut
348              
349             sub function_exists {
350 3 50   3 1 1417 my $self = ref $_[0] ? shift : return undef;
351 3         13 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<methods()>>, that is, 'public',
365             'private', 'full' and 'expanded', and combinations thereof.
366              
367             =cut
368              
369             sub methods {
370 0 0   0 1 0 my $self = ref $_[0] ? shift : return undef;
371 0         0 Class::Inspector->methods( $self->{name}, @_ );
372             }
373              
374             =pod
375              
376             =head2 subclasses
377              
378             The C method will search then entire namespace (and thus
379             B 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 on the class as a static
383             method. (i.e. C<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 if the class name
387             provided is invalid.
388              
389             =cut
390              
391             sub subclasses {
392 1 50   1 1 471 my $self = ref $_[0] ? shift : return undef;
393 1         6 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 method is a straight pass through to the
408             C function. Returns an ordered list of
409             class names, with no duplicates. The list does NOT include the class itself,
410             or the L class.
411              
412             =cut
413              
414             sub super_path {
415 0 0   0 1   my $self = ref $_[0] ? shift : return undef;
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.
425              
426             =cut
427              
428             sub self_and_super_path {
429 0 0   0 1   my $self = ref $_[0] ? shift : return undef;
430 0           Class::ISA::self_and_super_path( $self->{name} );
431             }
432              
433             =pod
434              
435             =head2 full_super_path
436              
437             The C method is an additional method not in C.
438             It returns as for C, except that it also contains BOTH the
439             class itself, and C. This full list is more technically accurate,
440             but less commonly used, and as such isn't available from L
441             itself.
442              
443             =cut
444              
445             sub full_super_path {
446 0 0   0 1   my $self = ref $_[0] ? shift : return ();
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, including import.
462             # Roughly equivalent to require $name; $name->import;
463             sub load {
464 0 0   0 0   my $self = shift or return undef;
465              
466             # Shortcut if the class is already loaded
467 0 0         return 1 if Class::Inspector->loaded( $self->{name} );
468              
469             # Get the resolved file name
470 0 0         my $filename = $self->resolved_filename() or return undef;
471              
472             # Load the class
473 0 0         require $filename or return undef;
474              
475             # Do we need to call an import method?
476 0 0         my $import = $self->can( 'import' ) or return 1;
477              
478             # Go to the import
479 0           goto &{$import};
  0            
480             }
481              
482             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
495              
496             For other inquiries, contact the author
497              
498             =head1 AUTHOR
499              
500             Adam Kennedy Eadamk@cpan.orgE, L
501              
502             =head1 SEE ALSO
503              
504             C, C, and C, 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