File Coverage

lib/AutoCode/SymbolTableUtils.pm
Criterion Covered Total %
statement 20 60 33.3
branch 0 18 0.0
condition 0 3 0.0
subroutine 7 12 58.3
pod 0 7 0.0
total 27 100 27.0


line stmt bran cond sub pod time code
1             package AutoCode::SymbolTableUtils;
2 7     7   92 use strict;
  7         26  
  7         358  
3             # It is right NOT to let this package inhert anything.
4              
5              
6             sub st_by_object {
7 0     0 0 0 my ($object)=@_;
8 7     7   31 no strict 'refs';
  7         11  
  7         2563  
9 0         0 foreach my $symbol(keys %{"$object\::"}){
  0         0  
10 0         0 print "$symbol\n";
11 0         0 my $full_name="$object\::$symbol";
12 0         0 foreach(qw(SCALAR ARRAY HASH CODE GLOB IO)){
13 0 0       0 if(defined (*$full_name{$_})){
14 0         0 print "\t$_\t". ${*$full_name{$_}} ."\n";
  0         0  
15             }
16             }
17 0         0 print ${"$object\::$symbol"}, "\n";
  0         0  
18 0         0 print @{"$object\::$symbol"}, "\n";
  0         0  
19 0 0       0 print "CODE: ". &{"$object\::$symbol"}, "\n" if defined &{"$object\::$symbol"};
  0         0  
  0         0  
20             }
21             }
22              
23             sub return_deref {
24 0     0 0 0 my ($symbol, $ref)=@_;
25 0         0 my %ref_prefix=(
26             SCALAR => '$',
27             ARRAY => '@',
28             HASH => '%',
29             CODE => '&',
30             GLOB => '*'
31             );
32 0         0 my $prefix=$ref_prefix{$ref};
33 0 0       0 unless(defined $prefix){
34             ; # something is warning
35             }
36              
37 0         0 my $eval="${prefix}$symbol";
38 0         0 return eval($eval);
39 0 0       0 if($ref eq 'SCALAR'){
    0          
    0          
    0          
    0          
40 0         0 $$symbol;
41             }elsif($ref eq 'ARRAY'){
42 0         0 @$symbol;
43             }elsif($ref eq 'HASH'){
44 0         0 %$symbol;
45             }elsif($ref eq 'CODE'){
46 0         0 &$symbol;
47             }elsif($ref eq 'GLOB'){
48 0         0 *$symbol;
49             }
50             }
51              
52             sub listsub {
53 0     0 0 0 my ($obj)=@_;
54 0   0     0 my $pkg = ref($obj) || $obj;
55 0         0 __PACKAGE__->_load_module($pkg);
56 0         0 my @subs;
57 7     7   40 no strict 'refs';
  7         9  
  7         642  
58 0         0 foreach (keys %{"$pkg\::"}){
  0         0  
59 0 0       0 push @subs, $_ if defined &$_;
60             }
61 0         0 return @subs;
62             }
63              
64             sub PKG_exists_in_ST {
65 2     2 0 9 my ($pkg) = @_;
66 7     7   33 no strict 'refs';
  7         10  
  7         434  
67 2         3 return (scalar keys %{"$pkg\::"});
  2         22  
68             }
69              
70             # There are more than one way to do such.
71             # $glob='AutoSQL::Root::new'
72             # no strict 'refs';
73             # defined *{$glob}{CODE}; # Way 1
74             # defined &{$glob}; # Way 2
75             sub CODE_exists_in_ST {
76 308     308 0 356 my ($glob)=@_;
77 7     7   30 no strict 'refs';
  7         11  
  7         760  
78 308         2032 return defined &$glob;
79             }
80              
81              
82             # There are differences between defined *{$glob}{CODE}; and defined &$glob;
83             #
84             # If $glob is a string, e.g. a scalar, then ,,,,,,
85             *detect_sub_in_symbol_table = \&CODE_exists_in_ST;
86             *code_exists=\&CODE_exists_in_ST;
87              
88             sub CODE_exists {
89 0     0 0   my $glob = shift;
90 0           return defined &$glob;
91             }
92              
93             sub ARRAY_exists {
94 0     0 0   my $glob=shift;
95 0           return defined &$glob;
96             }
97              
98             1;
99              
100             __END__