File Coverage

blib/lib/Dynamic/Loader.pm
Criterion Covered Total %
statement 73 86 84.8
branch 21 42 50.0
condition 2 7 28.5
subroutine 10 12 83.3
pod 5 6 83.3
total 111 153 72.5


line stmt bran cond sub pod time code
1             package Dynamic::Loader;
2 3     3   24111 use strict;
  3         6  
  3         166  
3              
4             require Exporter;
5 3     3   19 use Carp qw/confess/;
  3         6  
  3         230  
6 3     3   873 use Env::Path;
  3         2837  
  3         25  
7 3     3   71 use File::Basename;
  3         6  
  3         4483  
8             require Data::Dumper if defined( $ENV{DEBUG} );
9              
10             our ( $VERSION, $BINPATH, @ISA, @EXPORT );
11             $VERSION = '1.08';
12              
13             =head1 NAME
14              
15             Dynamic::Loader - call a script without to know where is his location.
16              
17              
18             =head1 SYNOPSIS
19              
20             The Dynamic::Loader manage the dynamic location of scripts and bundles.
21             Scripts and bundles are packaged in there own directory.
22             The bundles and scripts locations are discribed on a named configuration file.
23             The prefix configuration directory can be specified by the $JAVAPERL environnement.
24             The default directory is $HOME/.perljava/conf, but you can specify a custom
25             prefix with the $JAVAPERL/conf variable.
26            
27             A configuration is .conf with this format:
28             prefix=
29             bin=
30             lib=
31              
32              
33             =head1 DEFAULT SCRIPT AND PARAMS
34              
35             When C is used, you can specify the script name and his options
36             command:
37             perl -S fromjar.pl scriptname.pl --a=... --b=...
38              
39              
40             =cut
41              
42             @ISA = qw(Exporter);
43             @EXPORT = qw($SCRIPTPATH $PATH $PERL5LIB &listScripts &getExecPrefix);
44             our ( $SCRIPTPATH, $PATH, $PERL5LIB, );
45              
46             sub import {
47 3     3   35 my $class = shift;
48              
49             #@_ contains what could be passed on -MLoader=...; iv ever
50 3         10 init();
51 3         626 $class->export_to_level( 1, $class, @_ );
52             }
53              
54             =head3 init()
55              
56             setup libs and bin directories
57              
58             #fix lib and script path according to what's given
59              
60             =cut
61              
62             sub init {
63 3     3 1 6 my $perlJavaHome;
64 3   66     29 $perlJavaHome = $ENV{PERLLOADERHOME} || $ENV{JAVAPERL};
65 3 100       19 $perlJavaHome = "$ENV{HOME}/.perljava" unless defined $perlJavaHome;
66              
67             #$ENV{PATH}='';
68 3         56 $PATH = Env::Path->PATH;
69 3         98 $SCRIPTPATH = Env::Path->SCRIPTPATH;
70 3         68 $PERL5LIB = Env::Path->PERL5LIB;
71              
72             #TODO change that from ENV
73 3         44 my @modules;
74             my %conffiles;
75 3 50       13 if ( $ENV{PERLLOADERMODULES} ) {
76 0         0 @modules = split /:/, $ENV{PERLLOADERMODULES};
77             }
78             else {
79 3         361 foreach (<$perlJavaHome/conf/*.conf>) {
80 2 50       81 open( CONFIGFILE, $_ ) or next;
81 2         8 my %entry = ();
82 2         28 while ( my $l = ) {
83 9 100       59 if ( $l =~ /^([^=]+)=(.*)/ ) {
84 8         23 my ( $key, $val ) = ( $1, $2 );
85 8 100       18 if ( $key eq "prefix" ) {
86 2         7 $conffiles{$val} = \%entry;
87 2         10 push @modules, $val;
88             }
89             else {
90 6         32 $entry{$key} = $val;
91             }
92             }
93             }
94 2         25 close CONFIGFILE;
95             }
96              
97             }
98 3 50       16 require Data::Dumper if defined( $ENV{DEBUG} );
99 3 50       18 printf Data::Dumper::Dumper( \%conffiles ) . "\n" if defined( $ENV{DEBUG} );
100 3         7 foreach my $pjar (@modules) {
101 2     2   155 eval "use lib \"$pjar/$conffiles{$pjar}->{lib}\"";
  2         1994  
  2         1655  
  2         11  
102             }
103              
104             #we wish to put the path from the given directory, but in the correct order, and in front of all other.
105 3         233 foreach my $pjar ( reverse @modules ) {
106 2         10 my $bin = "$pjar/$conffiles{$pjar}->{bin}";
107 2         9 $bin =~ s/\/\//\//g;
108 2 50       12 $SCRIPTPATH->Prepend($bin) unless $SCRIPTPATH->Contains($bin);
109 2 50       123 $PATH->Prepend($bin) unless $PATH->Contains($bin);
110 2         187 my $lib = "$pjar/$conffiles{$pjar}->{lib}";
111 2         8 $lib =~ s/\/\//\//g;
112 2 50       9 $PERL5LIB->Prepend($lib) unless $PERL5LIB->Contains($lib);
113             }
114              
115             }
116              
117             =head3 Dynamic::Loader::listScripts([patt])
118              
119             Return a list of commands following a pattern listScripts(), listScripts("*.pl"), listScripts("phe*")
120              
121             The commands returned here are returned with a relative path to the package they belong to
122              
123             =cut
124              
125             sub listScripts {
126 1     1 1 1385 require File::Find::Rule;
127 0   0     0 my $patt = shift || '*';
128              
129 0         0 my @tmp;
130 0         0 foreach my $p ( $SCRIPTPATH->List ) {
131 0         0 foreach ( File::Find::Rule->file()->name($patt)->in($p) ) {
132 0 0       0 next if /\/\.svn\//;
133 0         0 s/^$p([\/\\])?//;
134 0         0 push @tmp, $_;
135             }
136             }
137 0         0 return @tmp;
138             }
139              
140             =head3 Dynamic::Loader::getScript(relative_path)
141              
142             Return the complete path to the given scripts.
143              
144             Contrary to listScripts(), this command must return exactly one script and will die if not;
145              
146             =cut
147              
148             sub getScript {
149 2 50   2 1 104240 my $relPath = shift or confess "no relative path given";
150 2         75 my @tmp;
151 2         30 foreach ( $SCRIPTPATH->List ) {
152 2         43 my $full = "$_/$relPath";
153 2 50       56 push @tmp, $full if -f $full;
154             }
155 2 50       16 confess "no script found for [$relPath]" unless @tmp;
156 2         7 my $contents;
157 2 50       12 if (@tmp) {
158 2         29 local $/;
159 2         11 foreach my $f (@tmp) {
160 2 50       71 open( FD, "<$f" ) or die "cannot read $f";
161 2         55 my $tmp = ;
162 2         20 close FD;
163 2 50       7 unless ($contents) {
164 2         10 $contents = $tmp;
165             }
166             else {
167 0 0       0 if ( $contents ne $tmp ) {
168 0 0       0 confess
169             "multiple scripts found with incompatible contents for [$relPath] in "
170             . join(@tmp)
171             if @tmp > 1;
172             }
173             }
174             }
175             }
176 2         7 return $tmp[0];
177             }
178              
179             =head3 Dynamic::Loader::getLibs(relative_path)
180              
181             Return the complete path to the given scripts + the complete perl prefix with perl5libs.
182              
183             =cut
184              
185             sub getLongScript {
186 1 50   1 0 267 my $relPath = shift or confess "no relative path given";
187 1         7 my $path = getScript($relPath);
188 1         3 my $p5l = "$^X ";
189 1         8 foreach ( $PERL5LIB->List ) {
190 3         20 $p5l .= "-I$_ ";
191             }
192              
193 1 50       6 printf "---> $p5l$path \n" if defined( $ENV{DEBUG} );
194 1         6 return "$p5l$path";
195             }
196              
197             =head3 Dynamic::Loader::getExecPrefix()
198              
199             return an array to prepend to execution (perl, includes etc...)
200              
201             =cut
202              
203             sub getExecPrefix {
204 0     0 1 0 return ($^X);
205             }
206              
207             =head3 Dynamic::Loader::whence([pat])
208              
209             return a list of commands with the full path corresponding to a pattern. Think of ls completion in bash
210              
211             =cut
212              
213             sub whence {
214 0   0 0 1 0 return $SCRIPTPATH->Whence( $_[0] or "*" );
215             }
216              
217             =head1 AUTHOR
218              
219             Olivier Evalet, C<< >>
220             Alexandre Masselo C<< >>
221              
222             =head1 BUGS
223              
224             Please report any bugs or feature requests to C, or through
225             the web interface at L. I will be notified, and then you'll
226             automatically be notified of progress on your bug as I make changes.
227              
228              
229              
230              
231             =head1 SUPPORT
232              
233             You can find documentation for this module with the perldoc command.
234              
235             perldoc Dynamic::Loader
236              
237              
238             You can also look for information at:
239              
240             =over 4
241              
242             =item * RT: CPAN's request tracker
243              
244             L
245              
246             =item * AnnoCPAN: Annotated CPAN documentation
247              
248             L
249              
250             =item * CPAN Ratings
251              
252             L
253              
254             =item * Search CPAN
255              
256             L
257              
258             =back
259              
260              
261             =head1 ACKNOWLEDGEMENTS
262              
263              
264             =head1 COPYRIGHT & LICENSE
265              
266             Copyright 2008 Olivier Evalet, Alexandre Masselot all rights reserved.
267              
268             This program is free software; you can redistribute it and/or modify it
269             under the same terms as Perl itself.
270              
271              
272             =cut
273              
274             1; # End of Dynamic::Loader