/usr/share/perl5/Dist/Zilla/Role/PPI.pm is in libdist-zilla-perl 5.020-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 | package Dist::Zilla::Role::PPI;
# ABSTRACT: a role for plugins which use PPI
$Dist::Zilla::Role::PPI::VERSION = '5.020';
use Moose::Role;
use Moose::Util::TypeConstraints;
use namespace::autoclean;
use Digest::MD5 qw(md5);
use Storable qw(dclone);
#pod =head1 DESCRIPTION
#pod
#pod This role provides some common utilities for plugins which use 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, our document is 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);
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.
#pod
#pod =cut
sub document_assigns_to_variable {
my ($self, $orig_document, $variable) = @_;
# Clone because ppi_document_for_file which the caller is likely to
# have retrieved his document from caches aggressively, and we'd
# like to prun POD and comments.
#
# It would be pretty stupid of us to say we found a variable in some
# comment or in the POD, which we might do because if the POD is
# preceded by __END__ or __DATA__ it'll be a PPI::Statement. So
# prune PPI::Statement::* things that we don't want, note that we
# don't have to prune e.g. PPI::Token::Pod because of the isa check
# in the finder below.
my $document = dclone($orig_document);
$document->prune($_) for qw(PPI::Statement::End PPI::Statement::Data);
my $finder = sub {
my $node = $_[1];
return 1 if $node->isa('PPI::Statement') && $node->content =~ /(?<!\\)\Q$variable\E\s*=/sm;
return 0;
};
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.020
=head1 DESCRIPTION
This role provides some common utilities for plugins which use 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.
=head1 AUTHOR
Ricardo SIGNES <rjbs@cpan.org>
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2014 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
|