File: Package.pm

package info (click to toggle)
libparse-cpan-packages-perl 2.40-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 160 kB
  • ctags: 50
  • sloc: perl: 382; makefile: 2
file content (79 lines) | stat: -rwxr-xr-x 2,009 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package Parse::CPAN::Packages::Package;
use Moo;

use PPI;
use Types::Standard qw( InstanceOf Str );

has 'package'      => ( is => 'rw', isa => Str );
has 'version'      => ( is => 'rw', isa => Str );
has 'prefix'       => ( is => 'rw', isa => Str );
has 'distribution' => ( is => 'rw', isa => InstanceOf ['Parse::CPAN::Packages::Distribution'] );

sub filename {
    my ( $self )     = @_;
    my $distribution = $self->distribution;
    my @filenames    = $distribution->list_files;
    my $package_file = $self->package;
    $package_file =~ s{::}{/}g;
    $package_file .= '.pm';
    my ( $filename ) = grep { /$package_file$/ } sort { length( $a ) <=> length( $b ) } @filenames;
    return $filename;
}

sub file_content {
    my ( $self ) = @_;
    my $filename = $self->filename;
    my $content  = $self->distribution->get_file_from_tarball( $filename );
    return $content;
}

sub subs {
    my ( $self ) = @_;

    my $document = PPI::Document->new( \( $self->file_content ) );
    my $subs = $document->find('PPI::Statement::Sub');

    return map { $_->name } @{$subs};
}

sub has_matching_sub {
    my ( $self, $sub_regex ) = @_;

    my @matching_subs = grep { $_ =~ $sub_regex } $self->subs;

    return @matching_subs;
}

1;

__END__

=head1 NAME

Parse::CPAN::Packages::Package - represents a CPAN package

=head1 DESCRIPTION

Represents a CPAN Package. Note: The functions filename and file_content work
only if a mirror directory was supplied for parsing or the package file was
situated inside a cpan mirror structure.

=head1 METHODS

=head2 filename

Tries to guess the name of the file containing this package by looking through
the files contained in the distribution it belongs to.

=head2 file_content

Tries to return the contents of the file returned by filename().

=head2 subs

Experimental function. Tries to return the names of all subs in the package.

=head2 has_matching_sub( $regex )

Experimental function. Tries to see if any sub name in the package matches the
regex.