blib/lib/PlugIn.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 130 CC 12 SCAR 26.5
CC
12
Cov
0%
Open
20.0
OpenEval
20.0
Read
20.0
CRAP 14.2
1 #################################
2 package PlugIn;
3
4 use Archive::Tar; # (mv0.071) Needed to Open TAR.GZ data files
5 #################################
6
7 $PlugIn::VERSION = '0.01';
8
9 #*********************************************************
10 #*********************************************************
11 sub Open
12 {
13 my $file = shift(@_); # Pass the file name of the plug-in file
14 my $switch = shift(@_); # Pass the optional switch to save the extracted file
15
16 if (!-e $file)
17 {
18 return;
19 }
20 if (my $tar = Archive::Tar->new($file, 1))
21 {
22 my @filelist = $tar->list_files();
23 foreach (@filelist)
24 {
25 $tar->extract($_);
26 require $_;
27 if (!$switch)
28 {
29 unlink $_;
30 }
31 }
32 }
33 else {
34 return;
35 }
36
37 return 1;
38 }
39 #*********************************************************
40 =pod
41 =cut
42 #*********************************************************
43 sub OpenEval
44 {
45 my $file = shift(@_); # Pass the file name of the plug-in file
46 my $switch = shift(@_); # Pass the optional switch to save the extracted file
47
48 if (!-e $file)
49 {
50 return;
51 }
52 if (my $tar = Archive::Tar->new($file, 1))
53 {
54 my @filelist = $tar->list_files();
55 foreach (@filelist)
56 {
57 $tar->extract($_);
58 my ($data);
59 open (IN, $_) || die "Error: Can't open $file\n";
60 read (IN, $data, (-s $_));
61 close IN;
62 eval ($data);
63 if (!$switch)
64 {
65 unlink $_;
66 }
67 }
68 }
69 else {
70 return;
71 }
72
73 return 1;
74 }
75 #*********************************************************
76 =pod
77 =cut
78 #*********************************************************
79 sub Read
80 {
81 my $file = shift(@_); # Pass the file name of the plug-in file
82 my $switch = shift(@_); # Pass the optional switch to save the extracted file
83 my %readfile; # Return hash to contain data
84
85 if (!-e $file)
86 {
87 return;
88 }
89 if (my $tar = Archive::Tar->new($file, 1))
90 {
91 my @filelist = $tar->list_files();
92 foreach (@filelist)
93 {
94 my ($data);
95 $tar->extract($_);
96 open (IN, $_) or return;
97 read (IN, $data, (-s $_));
98 close IN;
99 $readfile{$_} = $data;
100 if (!$switch)
101 {
102 unlink $_;
103 }
104 }
105 }
106 else {
107 return;
108 }
109 return %readfile;
110 }
111 #*********************************************************
112 =pod
113 =cut
114 #*********************************************************
115 sub Write
116 {
117 my $output = shift(@_); # Pass the name of the output Data file
118 my @files = @_; # Pass the list of files to be written to the Data file
119 my @filelist; # The list of filenames to be returned
120
121 my $tar = Archive::Tar->new();
122 my $num = $tar->add_files(@files);
123 @filelist = $tar->list_files();
124 $tar->write($output, 1);
125
126 return @filelist;
127 }
128 #*********************************************************
129 =pod
130 =cut
131 #*********************************************************
132 sub WriteEval
133 {
134 my $output = shift(@_); # Pass the name of the output Data file
135 my @files = @_; # Pass the list of files to be written to the Data file
136 my @filelist; # The list of filenames to be returned
137
138 if (!EvalIt(@files))
139 {
140 my $tar = Archive::Tar->new();
141 my $num = $tar->add_files(@files);
142 @filelist = $tar->list_files();
143 $tar->write($output, 1);
144 }
145 else {
146 return;
147 }
148
149 return @filelist;
150 }
151 #*********************************************************
152 =pod
153 =cut
154 #*********************************************************
155 sub EvalIt
156 {
157 my (@files) = @_;
158 my ($data, $return);
159
160 foreach my $file (@files)
161 {
162 open (IN, $file) or return;
163 read (IN, $data, (-s $file));
164 close IN;
165 if (eval ($data))
166 {
167 $return = $return | 0;
168 }
169 else {
170 $return = $return | 1;
171 }
172 }
173 return $return;
174 }
175 =head1 NAME
176
177 PlugIn - Create and Access TAR.GZ files containing executible Perl code for dynamic execution
178
179 =head1 SYNOPSIS
180
181 use PlugIn;
182 my $fileswritten = Write('prog.dat', 'prog1.pl', 'prog2.pl');
183 print "$fileswritten files written...\n";
184 my %filesread = Read('prog.dat', 1);
185 Open('prog.dat', 1);
186 OpenEval('prog.dat');
187
188 =head1 ABSTRACT
189
190 PlugIn is a convienient way to store 'required' perl scripts in a single accessible file. It is also
191 a way to obscure source code from prying eyes by adding your own encryption or obscurement routines.
192
193 Since 'required' modules are accessed at run-time, PlugIn can also be used to dynamically configure perl
194 scripts that have been compiled using one of the commercial Perl compilers. This gives you the ability
195 to create a free-standing EXE, but keep it dynamically configurable through the use of a plug-in data file
196 without the need for re-compilation and distribution!
197
198 The current version of PlugIn is available at:
199
200 http://home.earthlink.net/~bsturner/perl/index.html
201
202 =head1 CREDITS
203
204 Thanks go to Indy Singh of WWW.DEMOBUILDER.COM for providing the 'eval' source to obscure run-time compiler messages.
205
206 =head1 HISTORY
207
208 0.01 First release
209
210 =head1 INSTALLATION
211
212 This module is shipped as a basic PM file that should be installed in your
213 site\lib dir. This module should work in any Perl port that has the
214 Archive::Tar module.
215
216 =head1 DEPENDENCIES
217
218 Archive::Tar must be installed for this module to function. Archive::Tar is used as the principle method for the creation
219 and reading of data files.
220
221 =head1 DESCRIPTION
222
223 To use this module put the following line at the beginning of your script:
224
225 use PlugIn;
226
227 Any one of the functions can be referenced by:
228
229 var = PlugIn::function
230
231 =head1 RETURN VALUES
232
233 Unless otherwise specified, all functions return undef if unsuccessful and non-zero data if successful.
234
235 =head1 FUNCTIONS
236
237 The following functions are available, but not exported.
238
239 =head2 PlugIn::Open
240
241 PlugIn::Open($file, $optswitch);
242
243 Opens a data file (TAR.GZ), requires it, and then deletes the extracted files by default. The code in each required
244 file is automatically executed and included subroutines are available.
245
246 $file is the filename of the data file to be opened
247 $optswitch is an optional boolean switch to prevent deletion of the extracted files
248
249 =head2 PlugIn::OpenEval
250
251 PlugIn::OpenEval($file, $optswitch);
252
253 Opens a data file (TAR.GZ), evaluates it, and then deletes the extracted files by default. The code in each required
254 file is automatically executed and included subroutines are available. The evaluation routine should hide run-time
255 compiler messages.
256
257 $file is the filename of the data file to be opened
258 $optswitch is an optional boolean switch to prevent deletion of the extracted files
259
260 =head2 PlugIn::Read
261
262 %hash = PlugIn::Read($file, $optswitch);
263
264 Opens a data file (TAR.GZ), extracts the contents, and then deletes them by default. The code is returned in the
265 specified hash.
266
267 %hash is the returned data with the keys listed as the extracted filename
268 $hash{filename} is the contents of the specified extracted file
269
270 $file is the filename of the data file to be opened
271 $optswitch is an optional boolean switch to prevent deletion of the extracted files
272
273 Read is provided as an accessor method if you want to add your own processing to the data before requiring it. The data
274 could be encrypted or otherwise obscured.
275
276 =head2 PlugIn::Write
277
278 @list = PlugIn::Write($datfile, @files);
279 $files = PlugIn::Write($datfile, @files);
280
281 Creates a new data file (TAR.GZ), with the specified list of files.
282
283 @list - in list context it returns the names of files added to the data file
284 $files - in scalar context it returns the number of files added to the data file
285
286 $datfile is the name of the data file to be created
287 @files is the list of file to be added to the archive
288
289 If you wish to store encrypted or obscured code, the input files must already be in this condition.
290
291 =head2 PlugIn::WriteEval
292
293 @list = PlugIn::WriteEval($datfile, @files);
294 $files = PlugIn::WriteEval($datfile, @files);
295
296 Before the data file is created, each file in the filelist is first evaluated for errors. If all files evaluate successfully
297 then a new data file (TAR.GZ) is created. Files are evaluated through the use of the PlugIn::EvalIt function.
298
299 @list - in list context it returns the names of files added to the data file
300 $files - in scalar context it returns the number of files added to the data file
301
302 $datfile is the name of the data file to be created
303 @files is the list of file to be added to the archive
304
305 =head2 PlugIn::EvalIt
306
307 PlugIn::EvalIt(@files);
308
309 The passed data is evaluated using Perl's 'eval' function and returns true only if all data passes. While this serves to
310 provide some quality assurance, it also servers to hide run-time compiler messages in the commercial Perl to EXE compilers.
311
312 @files is the list of files to be evaluated
313
314 Based on code by Indy Singh of WWW.DEMOBUILDER.COM (Perl2EXE)
315
316 =head1 AUTHOR
317
318 Brad Turner ( I<bsturner@sprintparanet.com> ).
319
320 =cut