File Coverage

blib/lib/Acme/UNIVERSAL/new.pm
Criterion Covered Total %
statement 9 42 21.4
branch 0 16 0.0
condition 0 3 0.0
subroutine 3 9 33.3
pod 4 5 80.0
total 16 75 21.3


line stmt bran cond sub pod time code
1             package Acme::UNIVERSAL::new;
2              
3 1     1   36137 use strict;
  1         5  
  1         47  
4 1     1   6 use warnings;
  1         2  
  1         219  
5              
6 1     1   7 use vars '$VERSION';
  1         2  
  1         1735  
7             $VERSION = '0.01';
8              
9             sub UNIVERSAL::new
10             {
11 0     0 0   my $class = get_class();
12 0           my $ref = get_ref();
13 0           bless $ref, $class;
14             }
15              
16             sub get_class
17             {
18 0     0 1   my ($root, $prefix) = @_;
19              
20 0 0         unless ($root)
21             {
22 0           $root = \%main::;
23 0           $prefix = '';
24             }
25              
26 0           my %symbols = get_symbols( $root );
27              
28 0           my @candidates;
29              
30 0           while ( my ($namespace, $name) = each %symbols )
31             {
32 0 0         next if $namespace eq 'main::';
33 0 0         next if $namespace eq '::';
34 0           my $fullname = $prefix . $name;
35 0 0         push @candidates, $fullname if has_constructor( $fullname );
36 0           push @candidates, get_class( $root->{ $namespace }, $fullname . '::' );
37             }
38              
39 0 0         return $candidates[ rand( @candidates ) ] unless $prefix;
40 0           return @candidates;
41             }
42              
43             sub has_constructor
44             {
45 0     0 1   my $symbol = shift;
46 0 0 0       return unless $symbol && $symbol =~ /^[A-Za-z]/;
47 0 0         return unless my $ctor = $symbol->can( 'new' );
48 0 0         return if $ctor == \&UNIVERSAL::new;
49 0           return 1;
50             }
51              
52             sub get_symbols
53             {
54 0     0 1   my $table = shift;
55 0           return map { my $name = $_; s/::$//; $name => $_ }
  0            
  0            
  0            
56 0           grep { /::$/ }
57             keys %$table;
58             }
59              
60             sub get_ref
61             {
62 0     0 1   my @refs = ( \(my $foo), {}, [], sub {}, do { local *FOO; \*FOO } );
  0     0      
  0            
  0            
63 0           return $refs[ rand( @refs ) ];
64             }
65              
66             1; # End of Acme::UNIVERSAL::new
67              
68             __END__