File Coverage

blib/lib/Module/MetaInfo.pm
Criterion Covered Total %
statement 77 98 78.5
branch 21 42 50.0
condition 1 3 33.3
subroutine 14 14 100.0
pod 0 3 0.0
total 113 160 70.6


line stmt bran cond sub pod time code
1             package Module::MetaInfo;
2             $VERSION = "0.03";
3 1     1   707 use warnings;
  1         2  
  1         32  
4 1     1   4 use strict;
  1         2  
  1         29  
5 1     1   5 use Carp;
  1         1  
  1         86  
6 1     1   4 use Cwd;
  1         2  
  1         64  
7 1     1   771 use Symbol;
  1         1029  
  1         66  
8 1     1   5 use File::Find;
  1         2  
  1         65  
9              
10 1     1   567 use Module::MetaInfo::AutoGuess;
  1         3  
  1         29  
11 1     1   569 use Module::MetaInfo::DirTree;
  1         3  
  1         40  
12 1     1   500 use Module::MetaInfo::ModList;
  1         3  
  1         28  
13              
14 1     1   6 use vars qw($AUTOLOAD);
  1         2  
  1         859  
15              
16             =head1 NAME
17              
18             Module::MetaInfo - Report meta information from perl module distribution files
19              
20             =head1 USAGE
21              
22             use Module::MetaInfo.pm;
23             $mod=new Module::MetaInfo(perl-module-file.tar.gz);
24             $desc=$mod->description();
25              
26             =head1 DESCRIPTION
27              
28             This module is designed to provide the primary interface to Perl meta
29             information for perl module distribution files (this, however, is a
30             prototype and hasn't yet been accepted by the "perl community", so
31             don't count on it yet). The module is designed to allow perl modules
32             to be easily and accurately packaged by other package systems such as
33             RPM and DPKG.
34              
35             The C module isn't actually designed to get any meta
36             information from the perl module. Instead it serves as an entry point
37             to other modules which have their own way of doing that. Since there
38             isn't yet any agreed way to store meta-information in perl modules
39             this may not be very reliable.
40              
41             Currently there are two ways of getting meta information: a) guessing
42             from the contents of the module and b) using a directory structure
43             which has not yet been accepted by the perl community. The default way
44             this module works is to first try b) then try a) then to give up.
45              
46             =head1 IMPLEMENTATION AND INHERITANCE..
47              
48             This module doesn't inherit anything. Instead it simply uses three
49             different classes C
50             C and C, first
51             trying functions from AutoGuess then from BestGuess and finally from
52             ModList.
53              
54             =head1 FUNCTIONS
55              
56             =head1 Module::MetaInfo::new( [)
57              
58             new creates the object and initialises it. The argument is the path
59             of the perl module distribution file.
60              
61             If you provide the perl modules list then meta information from the
62             modules list will be available.
63              
64             =cut
65              
66             my $scratch_dir="/tmp/perl-metainfo-temp."
67             . ( $ENV{LOGNAME} ? $ENV{LOGNAME} : ( $ENV{USER} ? $ENV{USER} : "dumb" ) );
68              
69             my $verbose=0;
70              
71             sub new {
72 5     5 0 268 my $s = shift;
73 5         25 my $distname = shift;
74 5         19 my $modlist = shift;
75 5   33     58 my $class = ref($s) || $s;
76 5         14 my $self={};
77              
78 5         62 my @metafinders = ( Module::MetaInfo::DirTree->new($distname),
79             Module::MetaInfo::AutoGuess->new($distname) );
80 5 100       172 push @metafinders, Module::MetaInfo::ModList->new($distname,$modlist)
81             if $modlist;
82 5         40 $self->{metafinders} = \@metafinders;
83              
84 5         22 $self->{_scratch_dir}=\$scratch_dir;
85 5         93 $self->{_verbose}=\$verbose;
86              
87 5         294 return bless $self, $class;
88             }
89              
90             =head1 $thing->::verbose() $thing->::scratch_dir()
91              
92             These functions affect class settings (or if called for an object,
93             only the settings of the object: afterwards that object will ignore
94             changes to the class settings).
95              
96             Currently implemented are verbose which prints debugging info and
97             scratch_dir which sets the directory to be used for unpacking perl
98             modules.
99              
100             =cut
101              
102             #N.B. $self->{scratch_dir} is a reference to the variable holding the
103             #location of he scratch directory.
104              
105             sub scratch_dir {
106 4     4 0 12229 my $self = shift;
107 4         16 my $val = shift;
108 4 50       19 confess "usage: thing->scratch_dir(level)" if @_;
109 4 50       23 if (ref($self)) {
110 0 0       0 return ${$self->{"_scratch_dir"}} unless defined $val;
  0         0  
111 0         0 $self->{"_scratch_dir"} = \$val; # just myself
112 0         0 foreach my $mod (@{$self->{metafinders}}) {
  0         0  
113 0 0       0 if ( $mod->can('scratch_dir') ) {
114 0         0 $mod->scratch_dir($val);
115             }
116             }
117 0         0 return ${$self->{"_scratch_dir"}};
  0         0  
118             } else {
119 4 50       17 return $scratch_dir unless defined $val;
120 4         17 $scratch_dir = $val; # whole class
121 4         20 return $scratch_dir;
122             }
123 0         0 die "not reached";
124             }
125              
126             #N.B. $self->{verbose} is a reference to the variable holding the
127             #location of he scratch directory.
128             sub verbose {
129 4     4 0 275 my $self = shift;
130 4         7 my $val = shift;
131 4 50       13 confess "usage: thing->verbose(level)" if @_;
132 4 50       13 if (ref($self)) {
133 0 0       0 return ${$self->{"_verbose"}} unless defined $val;
  0         0  
134 0         0 $self->{"_verbose"} = \$val; # just myself
135 0         0 foreach my $mod (@{$self->{metafinders}}) {
  0         0  
136 0 0       0 if ( $mod->can('verbose') ) {
137 0         0 $mod->verbose($val);
138             }
139             }
140 0         0 return ${$self->{"_verbose"}};
  0         0  
141             } else {
142 4 50       11 return $verbose unless defined $val;
143 4         7 $verbose = $val; # whole class
144 4         10 return $verbose;
145             }
146 0         0 die "not reached";
147             }
148              
149              
150             =head1 description / docs / etc...
151              
152             These functions provide meta information. They are provided by the
153             base classes. In all cases the functions will return C if they
154             are unable to get meta information of that kind. If there is no
155             function then MetaInfo will die (croak). If you want to work with
156             different versions of MetaInfo that may implement different sets of
157             functions then use C to catch the C.
158              
159             For more information about the different functions available, see the
160             subsidiary packages such as C and
161             C.
162              
163             N.B. we will throw an exception if you call a function which isn't
164             provided by any of the modules. This is important since some
165             functions are only present if a modules list has been provided to new.
166             Either don't call those functions unless you have provided the modules
167             list or catch the exception.
168              
169             =head1 RETURN CONVENTIONS
170              
171             Meta information function either return a scalar (name / description)
172             or an array value. If an array is returned, it will actually be
173             returned as a reference to an array if the function is used in a
174             scalar context.
175              
176             In C the scalar return is always used so any
177             modules used by it must provide this mode.
178              
179              
180             =head1 FUTURE FUNCTIONS
181              
182             There are a number of other things which should be implemented. These
183             can be guessed from looking at the possible meta-information which can
184             be stored in the RPM or DPG formats, for example. Examples include:
185              
186             =over 4
187              
188             =item *
189              
190             copyright - GPL / as perl / redistributable / etc.
191              
192             =item *
193              
194             application area - Database / Internet / WWW / HTTP etc.
195              
196             =item *
197              
198             suggests - related applications
199              
200             =back
201              
202             In many cases this data is generated currently by package building
203             tools simply by using a fixed string. The function should do better
204             than that in almost all cases or else it is't worth having...
205              
206             =head1 COPYRIGHT
207              
208             You may distribute under the terms of either the GNU General Public
209             License, version 2 or (at your option) later or the Artistic License,
210             as specified in the Perl README.
211              
212             =head1 BUGS
213              
214             Please see bugs in sub modules. Especially warnings in
215             C.
216              
217             =head1 AUTHOR
218              
219             Michael De La Rue.
220              
221             =head1 SEE ALSO
222              
223             L
224              
225             =cut
226              
227              
228             sub AUTOLOAD {
229 10     10   4099 (my $sub = $AUTOLOAD) =~ s/.*:://;
230 10 50       40 $sub =~ m/^DESTROY$/ && return;
231 10         93 my $self=shift;
232 10 50       27 croak "Function call into Module::MetaInfo wasn't a method call"
233             unless ref $self;
234 10         17 my $tried=0;
235 10         13 my $return=undef;
236 10         18 FINDER: foreach my $mod (@{$self->{metafinders}}) {
  10         57  
237 20 50       36 print "try $sub in " . ref ($mod) . "\n" if ${$self->{'_verbose'}};
  20         157  
238 20 100       170 if ( $mod->can($sub) ) {
239 16         26 $tried++;
240             #how should I cascade the effect of wantarray efficiently?
241 16         110 my $return=$mod->$sub();
242 16 100       81 if (defined $return) {
243 9         33 my $ref=ref $return;
244 9 100       32 if ($ref) {
245 3 50       48 die "meta info functions should only return array refs"
246             unless $ref=~m/^ARRAY/;
247 3 50       32 return wantarray ? @$return : $return;
248             }
249 6         48 return $return;
250             }
251             }
252             }
253 1         495 print STDERR "no metainfo module could provide $sub\n"
254 1 50       6 if ${$self->{'_verbose'}};
255 1 50       16 return undef if $tried;
256 0           die "No function $sub defined for retrieving meta information";
257             }
258              
259             42;
260