#!/usr/bin/perl use 5.006; use strict; use PerlReq::Utils qw(argv explode inc mod2path path2mod path2dep sv_version verf); use autouse qw(Pod::Usage pod2usage); use Getopt::Long 2.24 qw(GetOptions :config gnu_getopt); GetOptions "v|verbose+" => \my $Verbose, "h|help" => sub { pod2usage("00") } or pod2usage(2); $Verbose = 2 if $ENV{RPM_SCRIPTS_DEBUG}; $| = 1; # list of provides my %prov; # begin process_file($_) foreach argv(); sub process_file { my $fname = shift; warn "# processing $fname\n" if $Verbose > 1; my ($prefix, $basename) = explode($fname); unless ($prefix) { warn "# $fname does not match any prefix\n" if $Verbose > 1; return; } else { warn "# $fname has prefix $prefix\n" if $Verbose > 1; } if ($fname =~ /\.p[lh]$/) { $prov{$basename}{prov} = 1; warn "# $fname has basename $basename\n" if $Verbose; return; } elsif ($basename =~ /\.pm$/) { $prov{$basename}{prov} = 1; warn "# $fname has basename $basename, checking version\n" if $Verbose; } elsif ($basename =~ /\.al$/) { warn "# $fname (autoloaded file SKIP)" if $Verbose > 1; return; } else { warn "# $fname has basename $basename (SKIP)\n"; return; } my $cur_pkg = "main"; my $re_pkg = qr/\b\w+(?:::\w+)*/; open my $fh, $fname or die "$0: $fname: $!\n"; local $_; while (<$fh>) { chomp; next if /^\s*#/; next if /^=\w/ .. (/^=cut/ or eof); last if /^__(DATA|END)__\b/; s/\s+#\s.*//; # strip comments if (/^\s*\{?\s*package\s+($re_pkg)\s*;/) { $cur_pkg = $1; warn "# package $cur_pkg at $fname line $.\n" if $Verbose > 1; next; } /\$(?:($re_pkg)::)?VERSION\s*\)?\s*=/ or next; # it's a version assignemnt my $pkg = $1; my $val = $'; if ($pkg and mod2path($pkg) ne $basename) { warn "# saw \$$pkg\::VERSION at $fname line $. (SKIP)\n"; next; } if (not $pkg and mod2path($cur_pkg) ne $basename) { warn "# saw \$VERSION in package $cur_pkg at $fname line $. (SKIP)\n"; next; } if ($val =~ /\b\d|\d\./) { # there's a version number my $v = extract_version($fname, $_, $pkg); $prov{$basename}{ver}{$v} = 1 if $v; last; } elsif ($val =~ /\$($re_pkg)::VERSION\s*;/) { # holy Jesus! they assign the version from another package warn "# saw \$VERSION reference to \$$1::VERSION at $fname line $.\n" if $Verbose; my $basename2 = mod2path($1); $prov{$basename}{ver} = $prov{$basename2}{ver} ||= {}; $prov{$basename}{vref} = $basename2; last; } else { warn "# VERSION assignment not recognized at $fname line $. (SKIP)\n"; warn "# $_\n"; } } } # end foreach my $k (sort { uc($a) cmp uc($b) } keys %prov) { next unless $prov{$k}{prov}; my @v = sort keys %{$prov{$k}{ver}||{}}; if (not @v and my $k2 = $prov{$k}{vref}) { my $m1 = path2mod($k); my $m2 = path2mod($k2); warn "# unresolved reference from \$$m1\::VERSION to \$$m2\::VERSION\n"; } if (@v) { print path2dep($k) . " = $_\n" for @v; } else { print path2dep($k) . "\n"; } } sub extract_version { my ($fname, $line, $pkg) = @_; warn "# extracting version at line $.:\n# $line\n" if $Verbose > 1; my $code = "$line\n; \$VERSION"; $code =~ s/\$\Q$pkg\::VERSION/\$VERSION/g if $pkg; use Safe; my $safe = Safe->new; $safe->permit_only(qw(:base_core :base_mem :base_orig entereval grepstart grepwhile mapstart mapwhile)); my $version = $safe->reval($code); goto bad if $@ or not $version; use B qw(svref_2object); our $perlbug32967 = \$version; my $v = sv_version(svref_2object(\$version)); if ($v) { $v = verf($v); warn "# VERSION $v at $fname line $.\n" if $Verbose; return $v; } bad: warn "# WARNING: failed to extract version at $fname line $.:\n"; warn "# $line\n"; warn "# $@\n" if $@; return; } __END__ =head1 NAME perl.prov - list what Perl source files provide =head1 SYNOPSIS B [B<-v>|B<--verbose>] [I...] =head1 DESCRIPTION This script calculates conventional name for each Perl source I specified on a command line, based on its location relative to standard Perl library paths; alternatively, a list of files is obtained from standard input, one file per line. F<*.pm>, F<*.pl>, and F<*.ph> files are processed (F<*.pm> files also suffer version extraction). The output of perl.prov is suitable for automatic dependency tracking (e.g. for RPM packaging). For example, F provides C (as of perl-5.8.6). perl.prov is a counterpart of L. =head1 OPTIONS =over =item B<-v>, B<--verbose> Increase verbosity. =back The RPM_PERL_LIB_PATH environment variable, if set, must contain the list of paths, separated by colons. These paths are considered as library paths used to determine relative names of provided perl files (in addition to paths from C<@INC> variable). =head1 AUTHOR Written by Alexey Tourbin , based on an earlier version by Ken Estes , with contributions from Mikhail Zabaluev . =head1 COPYING Copyright (c) 2003, 2004 Alexey Tourbin, ALT Linux Team. This is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. =head1 SEE ALSO L