1 |
dpavlin |
24 |
# Dobrica Pavlinusic, <dpavlin@rot13.org> 06/06/07 12:04:23 CEST |
2 |
|
|
|
3 |
|
|
package Perly::Depends; |
4 |
|
|
|
5 |
|
|
=head1 NAME |
6 |
|
|
|
7 |
|
|
Perly::Depends |
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION |
10 |
|
|
|
11 |
|
|
Code to parse dependecy from file, based on L<Module::Dependency::Indexer> C<_parseFile>. |
12 |
|
|
|
13 |
|
|
=head1 METHODS |
14 |
|
|
|
15 |
|
|
=cut |
16 |
|
|
|
17 |
|
|
use strict; |
18 |
|
|
use warnings; |
19 |
|
|
|
20 |
|
|
=head2 parse |
21 |
|
|
|
22 |
|
|
my $tree = Perly::Depends->parse( $source ); |
23 |
|
|
|
24 |
|
|
=cut |
25 |
|
|
|
26 |
|
|
sub parse { |
27 |
|
|
my $self = shift; |
28 |
|
|
|
29 |
|
|
my $source = shift || die "no source?"; |
30 |
|
|
|
31 |
|
|
$self = { |
32 |
|
|
depends_on => [], |
33 |
|
|
depended_upon_by => [], |
34 |
|
|
}; |
35 |
|
|
|
36 |
dpavlin |
25 |
my %seen = ( strict => 1 ); |
37 |
dpavlin |
24 |
|
38 |
|
|
my $in_pod; |
39 |
|
|
my $l = 0; |
40 |
|
|
foreach ( split(/[\n\r]/, $source) ) { |
41 |
|
|
$l++; |
42 |
|
|
s/\r?\n$//; |
43 |
|
|
if ($in_pod) { |
44 |
|
|
$in_pod = 0 if /^=cut/; |
45 |
|
|
next; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
# get the package name |
49 |
|
|
if (m/^\s*package\s+([\w\:]+)\s*;/) { |
50 |
|
|
# XXX currently only record the first package seen |
51 |
|
|
if (exists $self->{'package'}) { |
52 |
|
|
warn "Can only index one package per file currently, ignoring $1 at line $l\n"; |
53 |
|
|
next; |
54 |
|
|
} |
55 |
|
|
$self->{'package'} = $1; |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
# get the dependencies |
59 |
|
|
if (m/^\s*use\s+([\w\:]+)/) { |
60 |
|
|
push( @{ $self->{'depends_on'} }, $1 ) unless ( $seen{$1}++ ); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
# get the dependencies |
64 |
|
|
if (m/^\s*require\s+([^\s;]+)/) { # "require Bar;" or "require 'Foo/Bar.pm' if $wibble;' |
65 |
|
|
my $required = $1; |
66 |
|
|
if ($required =~ m/^([\w\:]+)$/) { |
67 |
|
|
push @{ $self->{'depends_on'} }, $required unless $seen{$required}++; |
68 |
|
|
} |
69 |
|
|
elsif ($required =~ m/^["'](.*?\.pm)["']$/) { # simple Foo/Bar.pm case |
70 |
|
|
($required = $1) =~ s/\.pm$//; |
71 |
|
|
$required =~ s!/!::!g; |
72 |
|
|
push @{ $self->{'depends_on'} }, $required unless $seen{$required}++; |
73 |
|
|
} |
74 |
|
|
else { |
75 |
|
|
warn "Can't interpret $_ at line $l\n" |
76 |
|
|
unless m!sys/syscall.ph! |
77 |
|
|
or m!dumpvar.pl! |
78 |
|
|
or $required =~ /^5\./; |
79 |
|
|
} |
80 |
|
|
} |
81 |
dpavlin |
28 |
=for later |
82 |
dpavlin |
24 |
# the 'base' pragma - SREZIC |
83 |
|
|
if (m/^\s*use\s+base\s+(.*)/) { |
84 |
|
|
require Safe; |
85 |
|
|
my $safe = new Safe; |
86 |
|
|
( my $list = $1 ) =~ s/\s+\#.*//; |
87 |
|
|
$list =~ s/[\r\n]//; |
88 |
|
|
while ( $list !~ /;\s*$/ && ( $_ = <FILE> ) ) { |
89 |
|
|
s/\s+#.*//; |
90 |
|
|
s/[\r\n]//; |
91 |
|
|
$list .= $_; |
92 |
|
|
} |
93 |
|
|
$list =~ s/;\s*$//; |
94 |
|
|
my (@mods) = $safe->reval($list); |
95 |
|
|
warn "Unable to eval $_ at line $l: $@\n" if $@; |
96 |
|
|
foreach my $mod (@mods) { |
97 |
|
|
push( @{ $self->{'depends_on'} }, $mod ) unless ( $seen{$mod}++ ); |
98 |
|
|
} |
99 |
|
|
} |
100 |
dpavlin |
28 |
=cut |
101 |
dpavlin |
24 |
$in_pod = 1 if m/^=\w+/ && !m/^=cut/; |
102 |
|
|
last if m/^\s*__(END|DATA)__/; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
return $self; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
1; |