File Coverage

blib/lib/Astro/App/Satpass2/Macro/Code.pm
Criterion Covered Total %
statement 27 51 52.9
branch 2 12 16.6
condition 2 9 22.2
subroutine 8 12 66.6
pod 5 5 100.0
total 44 89 49.4


line stmt bran cond sub pod time code
1             package Astro::App::Satpass2::Macro::Code;
2              
3 20     20   1636 use 5.008;
  20         66  
4              
5 20     20   108 use strict;
  20         41  
  20         422  
6 20     20   103 use warnings;
  20         43  
  20         704  
7              
8 20     20   134 use parent qw{ Astro::App::Satpass2::Macro };
  20         66  
  20         115  
9              
10 20         2196 use Astro::App::Satpass2::Utils qw{
11             expand_tilde
12             load_package
13             quoter
14             @CARP_NOT
15 20     20   1480 };
  20         55  
16 20     20   159 use File::Spec;
  20         47  
  20         4643  
17              
18             our $VERSION = '0.051_01';
19              
20             sub init {
21 1     1 1 3 my ( $self ) = @_;
22 1         8 $self->SUPER::init();
23 1         11 my $parent = $self->parent();
24 1         5 my %popt = ( complaint => 'wail', fatal => 'wail' );
25             exists $self->{lib}
26 1 50       7 and $popt{lib} = $self->expand_tilde( $self->{lib} );
27             defined $self->{lib}
28             and not $self->{relative}
29             and not $self->{lib} =~ m/ \A ~ /smx
30 1 50 33     7 and $self->{lib} = File::Spec->rel2abs( $self->{lib} );
      33        
31 1         9 my $module = $self->load_package(
32             \%popt, $self->name(), 'Astro::App::Satpass2::Macro::Code'
33             );
34 0 0         $module->isa( 'Astro::App::Satpass2' )
35             or $self->wail( "$module is not a subclass of Astro::App::Satpass2" );
36              
37 0           my %implements; # Names and references to found code
38 0           my $stb = "${module}::"; # Name of loaded symbol table
39              
40             # Fairly deep magic begins here. We need symbolic references to
41             # traverse the symbol table of the loaded code, so:
42 20     20   161 no strict qw{ refs };
  20         47  
  20         6576  
43              
44 0           foreach my $name ( keys %$stb ) {
45 0           my $val = $stb->{$name};
46              
47             # We are only interested in symbols that start with word
48             # characters, excluding '_'
49 0 0 0       $name =~ m/ \A \w /smx
50             and not $name =~ m/ \A _ /smx
51             or next;
52              
53             # We need a reference to the entry's glob, which we obtain by
54             # symbolic reference.
55 0           my $glob = \$val;
56              
57             # If the code slot is empty we ignore it.
58 0           *{$glob}{CODE}
59 0 0         or next;
60              
61             # If the code does not have the Verb() attribute, we ignore it.
62             # TODO technically we have an encapsulation failure here which
63             # needs to be fixed up.
64 0 0         $parent->__get_attr( *{$glob}{CODE}, 'Verb' )
  0            
65             or next;
66              
67             # Record the fact that the module defines this name.
68 0           $implements{$name} = *{$glob}{CODE};
  0            
69             }
70              
71             # End of symbol table magic.
72 0           $self->{implements} = \%implements;
73 0           return;
74             }
75              
76             sub execute {
77 0     0 1   my ( $self, $name, @args ) = @_;
78 0           my $code = $self->implements( $name, required => 1 );
79 0           return $code->( $self->parent(), @args );
80             }
81              
82             sub has_lib {
83 0     0 1   my ( $self ) = @_;
84 0           return exists $self->{lib};
85             }
86              
87             sub lib {
88 0     0 1   my ( $self ) = @_;
89 0           return $self->{lib};
90             }
91              
92             sub relative {
93 0     0 1   my ( $self ) = @_;
94 0           return $self->{relative};
95             }
96              
97             1;
98              
99             __END__