/usr/share/perl5/Dist/Zilla/Role/PPI.pm is in libdist-zilla-perl 5.043-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | package Dist::Zilla::Role::PPI;
# ABSTRACT: a role for plugins which use PPI
$Dist::Zilla::Role::PPI::VERSION = '5.043';
use Moose::Role;
use Digest::MD5 qw(md5);
use namespace::autoclean;
#pod =head1 DESCRIPTION
#pod
#pod This role provides some common utilities for plugins which use L<PPI>.
#pod
#pod =method ppi_document_for_file
#pod
#pod my $document = $self->ppi_document_for_file($file);
#pod
#pod Given a dzil file object (anything that does L<Dist::Zilla::Role::File>), this
#pod method returns a new L<PPI::Document> for that file's content.
#pod
#pod Internally, this method caches these documents. If multiple plugins want a
#pod document for the same file, this avoids reparsing it.
#pod
#pod =cut
my %CACHE;
sub ppi_document_for_file {
my ($self, $file) = @_;
my $encoded_content = $file->encoded_content;
# We cache on the MD5 checksum to detect if the document has been modified
# by some other plugin since it was last parsed, making our document invalid.
my $md5 = md5($encoded_content);
return $CACHE{$md5}->clone if $CACHE{$md5};
require PPI::Document;
my $document = PPI::Document->new(\$encoded_content)
or Carp::croak(PPI::Document->errstr . ' while processing file ' . $file->name);
return ($CACHE{$md5} = $document)->clone;
}
#pod =method save_ppi_document_to_file
#pod
#pod my $document = $self->save_ppi_document_to_file($document,$file);
#pod
#pod Given a L<PPI::Document> and a dzil file object (anything that does
#pod L<Dist::Zilla::Role::File>), this method saves the serialized document in the
#pod file.
#pod
#pod It also updates the internal PPI document cache with the new document.
#pod
#pod =cut
sub save_ppi_document_to_file {
my ($self, $document, $file) = @_;
my $new_content = $document->serialize;
$file->encoded_content($new_content);
$CACHE{ md5($new_content) } = $document->clone;
}
#pod =method document_assigns_to_variable
#pod
#pod if( $self->ppi_document_for_file($document, '$FOO')) { ... }
#pod
#pod This method returns true if the document assigns to the given variable (the
#pod sigil must be included).
#pod
#pod =cut
sub document_assigns_to_variable {
my ($self, $document, $variable) = @_;
my $package_stmts = $document->find('PPI::Statement::Package');
my @namespaces = map { $_->namespace } @{ $package_stmts || []};
my ($sigil, $varname) = ($variable =~ m'^([$@%*])(.+)$');
my $package;
my $finder = sub {
my $node = $_[1];
if ($node->isa('PPI::Statement')
&& !$node->isa('PPI::Statement::End')
&& !$node->isa('PPI::Statement::Data')) {
if ($node->isa('PPI::Statement::Variable')) {
return (grep { $_ eq $variable } $node->variables) ? 1 : undef;
}
return 1 if grep {
my $child = $_;
$child->isa('PPI::Token::Symbol')
and grep {
$child->canonical eq "${sigil}${_}::${varname}"
and $node->content =~ /\Q${sigil}${_}::${varname}\E.*=/
} @namespaces
} $node->children;
}
return 0; # not found
};
my $rv = $document->find_any($finder);
Carp::croak($document->errstr) unless defined $rv;
return $rv;
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
Dist::Zilla::Role::PPI - a role for plugins which use PPI
=head1 VERSION
version 5.043
=head1 DESCRIPTION
This role provides some common utilities for plugins which use L<PPI>.
=head1 METHODS
=head2 ppi_document_for_file
my $document = $self->ppi_document_for_file($file);
Given a dzil file object (anything that does L<Dist::Zilla::Role::File>), this
method returns a new L<PPI::Document> for that file's content.
Internally, this method caches these documents. If multiple plugins want a
document for the same file, this avoids reparsing it.
=head2 save_ppi_document_to_file
my $document = $self->save_ppi_document_to_file($document,$file);
Given a L<PPI::Document> and a dzil file object (anything that does
L<Dist::Zilla::Role::File>), this method saves the serialized document in the
file.
It also updates the internal PPI document cache with the new document.
=head2 document_assigns_to_variable
if( $self->ppi_document_for_file($document, '$FOO')) { ... }
This method returns true if the document assigns to the given variable (the
sigil must be included).
=head1 AUTHOR
Ricardo SIGNES <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Ricardo SIGNES.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|