File Coverage

blib/lib/Hub/Base/Package.pm
Criterion Covered Total %
statement 15 74 20.2
branch 0 24 0.0
condition 0 5 0.0
subroutine 5 11 45.4
pod 4 4 100.0
total 24 118 20.3


line stmt bran cond sub pod time code
1             package Hub::Base::Package;
2 1     1   5 use strict;
  1         2  
  1         29  
3 1     1   4 use Hub qw/:lib/;
  1         2  
  1         5  
4             our ($AUTOLOAD);
5             our $VERSION = '4.00043';
6             our @EXPORT = qw//;
7             our @EXPORT_OK = qw/modexec/;
8 1     1   5 use constant RTMOD_NAME => 'module.pm'; # Default runtime module name
  1         41  
  1         46  
9 1     1   4 use constant RTMOD_INVOKE => 'run'; # Default runtime invokation method
  1         1  
  1         419  
10              
11             # ------------------------------------------------------------------------------
12             # modexec - Execute runtime module
13             # ------------------------------------------------------------------------------
14              
15             sub modexec {
16 0     0 1   my $opts = Hub::opts(\@_, {
17             filename => RTMOD_NAME,
18             method => RTMOD_INVOKE,
19             });
20 0 0         $$opts{'method'} = RTMOD_INVOKE unless defined $$opts{'method'};
21 0   0       my $args = shift || [];
22 0           my $path = Hub::srcpath($$opts{'filename'});
23 0 0         if ($path) {
24 0           my $pkg = mkinst('Package', $path);
25 0           return $pkg->call($$opts{'method'}, @$args);
26             } else {
27 0           confess ("Module not found: $$opts{'filename'}");
28             }#if
29             }#modexec
30              
31             # ------------------------------------------------------------------------------
32             # new - Constructor
33             # new $module_filename
34             # This creates a singleton adapter of the perl module
35             # ------------------------------------------------------------------------------
36              
37             sub new {
38 0     0 1   my $self = shift;
39 0 0         my $path = shift or confess "Filename required";
40 0   0       my $classname = ref($self) || $self;
41 0 0         my $filename = Hub::abspath($path)
42             or confess "Module does not exist: $path";
43 0           my $object = Hub::fhandler($filename, $classname);
44 0 0         unless( $object ) {
45 0           my $workdir = Hub::getpath($filename);
46 0           my $package = $filename;
47 0           $package =~ s/[\s\W]/_/g;
48 0           $self = {
49             'filename' => $filename,
50             'package' => $package,
51             'workdir' => $workdir,
52             };
53 0           $object = bless $self, $classname;
54 0           Hub::fattach($filename, $object);
55             }#unless
56 0           return $object;
57             }#new
58              
59             # ------------------------------------------------------------------------------
60             # call - Call a method in the underlying package
61             # call $method, [@parameters]
62             # Note that wrapped methods do not pass the 'defined' test
63             # ------------------------------------------------------------------------------
64              
65             sub call {
66 0     0 1   my $self = shift;
67 0 0         my $classname = ref($self) or croak "Illegal call to instance method";
68 0 0         my $method = shift or croak "Method required";
69 0           my $sub = $$self{'package'} . '::' . $method;
70 1     1   6 no strict 'refs';
  1         2  
  1         582  
71 0           Hub::pushwp($$self{'workdir'});
72 0           my $result = &$sub(@_);
73 0           Hub::popwp();
74 0           return $result;
75             }#call
76              
77             # ------------------------------------------------------------------------------
78             # AUTOLOAD - Proxy the call to the underlying package
79             # ------------------------------------------------------------------------------
80              
81             sub AUTOLOAD {
82 0     0     my $self = shift;
83 0 0         my $classname = ref($self) or croak "Illegal call to instance method";
84 0           my $name = $AUTOLOAD;
85 0 0         if( $name =~ /::(\w+)$/ ) {
86 0           return $self->call($1, @_);
87             } else {
88 0           die "Unhandled AUTOLOAD name";
89             }#if
90             }#AUTOLOAD
91              
92             # ------------------------------------------------------------------------------
93             # DESTROY - Defining this function prevents it from being searched in AUTOLOAD
94             # ------------------------------------------------------------------------------
95              
96 0     0     sub DESTROY {
97             }#DESTROY
98              
99             # ------------------------------------------------------------------------------
100             # reload - Callback method from L
101             # reload $file_instance
102             # Called implicty on the first attachment or when the file has been modified
103             # on disk. Not to be used unless you override L.
104             #
105             # Special patterns:
106             #
107             # package PACKAGE; # for dynamically allocating based on full path
108             #
109             # import 'foo.pm' as 'FOO'; # for including dynamic packages
110             # FOO::method();
111             # ------------------------------------------------------------------------------
112              
113             sub reload {
114 0     0 1   my $self = shift;
115 0 0         my $classname = ref($self) or croak "Illegal call to instance method";
116 0 0         my $instance = shift or croak "FileCache file-instance hash required";
117             #warn "file=$self->{'filename'}\n";
118             #warn " pkg=$self->{'package'}\n";
119 0           my $contents = $$instance{'contents'};
120 0           my %imports = ();
121 0           Hub::pushwp($$self{'workdir'});
122 0           $contents =~ s/\bPACKAGE\b/$self->{'package'}/mg;
123 0           $contents =~ s/^\s*IMPORT\s+['"]([^'"]+)['"]\s+AS\s+['"]([A-Z]+)['"];\s*$/
124 0           my $fn = $1;
125 0           my $alias = $2;
126 0           my $pkg = Hub::srcpath("$fn");
127 0           $pkg =~ s#[\s\W]#_#g;
128 0           $imports{$alias} = $pkg;
129 0           "Hub::mkinst('Package', Hub::srcpath('$fn'));\n"/mgei;
130 0           foreach my $k (keys %imports) {
131 0           $contents =~ s/\b$k\b/$imports{$k}/mg;
132             }
133 0           local $!;
134 0           eval $contents;
135 0           Hub::popwp();
136 0 0         if( $@ ) {
137 0           my $error = $@;
138 0           my ($eval_number) = $error =~ s/\(eval (\d+)\)/$$instance{'filename'}/;
139 0           die $error;
140             }#if
141             }#reload
142              
143              
144             # ------------------------------------------------------------------------------
145             1;