/usr/share/perl5/PPI/Document/Normalized.pm is in libppi-perl 1.220-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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | package PPI::Document::Normalized;
=pod
=head1 NAME
PPI::Document::Normalized - A normalized Perl Document
=head1 DESCRIPTION
A C<Normalized Document> object is the result of the normalization process
contained in the L<PPI::Normal> class. See the documentation for
L<PPI::Normal> for more information.
The object contains a version stamp and function list for the version
of L<PPI::Normal> used to create it, and a processed and delinked
L<PPI::Document> object.
Typically, the Document object will have been mangled by the normalization
process in a way that would make it fatal to try to actually DO anything
with it.
Put simply, B<never> use the Document object after normalization.
B<YOU HAVE BEEN WARNED!>
The object is designed the way it is to provide a bias towards false
negatives. A comparison between two ::Normalized object will only return
true if they were produced by the same version of PPI::Normal, with the
same set of normalization functions (in the same order).
You may get false negatives if you are caching objects across an upgrade.
Please note that this is done for security purposes, as there are many
cases in which low layer normalization is likely to be done as part of
a code security process, and false positives could be highly dangerous.
=head1 METHODS
=cut
# For convenience (and since this isn't really a public class), import
# the methods we will need from Scalar::Util.
use strict;
use Scalar::Util qw{refaddr reftype blessed};
use Params::Util qw{_INSTANCE _ARRAY};
use PPI::Util ();
use vars qw{$VERSION};
BEGIN {
$VERSION = '1.220';
}
use overload 'bool' => \&PPI::Util::TRUE;
use overload '==' => 'equal';
#####################################################################
# Constructor and Accessors
=pod
=head2 new
The C<new> method is intended for use only by the L<PPI::Normal> class,
and to get ::Normalized objects, you are highly recommended to use
either that module, or the C<normalized> method of the L<PPI::Document>
object itself.
=cut
sub new {
my $class = shift;
my %args = @_;
# Check the required params
my $Document = _INSTANCE($args{Document}, 'PPI::Document') or return undef;
my $version = $args{version} or return undef;
my $functions = _ARRAY($args{functions}) or return undef;
# Create the object
my $self = bless {
Document => $Document,
version => $version,
functions => $functions,
}, $class;
$self;
}
sub _Document { $_[0]->{Document} }
=pod
=head2 version
The C<version> accessor returns the L<PPI::Normal> version used to create
the object.
=cut
sub version { $_[0]->{version} }
=pod
=head2 functions
The C<functions> accessor returns a reference to an array of the
normalization functions (in order) that were called when creating
the object.
=cut
sub functions { $_[0]->{functions} }
#####################################################################
# Comparison Methods
=pod
=head2 equal $Normalized
The C<equal> method is the primary comparison method, taking another
PPI::Document::Normalized object, and checking for equivalence to it.
The C<==> operator is also overload to this method, so that you can
do something like the following:
my $first = PPI::Document->load('first.pl');
my $second = PPI::Document->load('second.pl');
if ( $first->normalized == $second->normalized ) {
print "The two documents are equivalent";
}
Returns true if the normalized documents are equivalent, false if not,
or C<undef> if there is an error.
=cut
sub equal {
my $self = shift;
my $other = _INSTANCE(shift, 'PPI::Document::Normalized') or return undef;
# Prevent multiple concurrent runs
return undef if $self->{processing};
# Check the version and function list first
return '' unless $self->version eq $other->version;
$self->_equal_ARRAY( $self->functions, $other->functions ) or return '';
# Do the main comparison run
$self->{seen} = {};
my $rv = $self->_equal_blessed( $self->_Document, $other->_Document );
delete $self->{seen};
$rv;
}
# Check that two objects are matched
sub _equal_blessed {
my ($self, $this, $that) = @_;
my ($bthis, $bthat) = (blessed $this, blessed $that);
$bthis and $bthat and $bthis eq $bthat or return '';
# Check the object as a reference
$self->_equal_reference( $this, $that );
}
# Check that two references match their types
sub _equal_reference {
my ($self, $this, $that) = @_;
my ($rthis, $rthat) = (refaddr $this, refaddr $that);
$rthis and $rthat or return undef;
# If we have seen this before, are the pointing
# is it the same one we saw in both sides
my $seen = $self->{seen}->{$rthis};
if ( $seen and $seen ne $rthat ) {
return '';
}
# Check the reference types
my ($tthis, $tthat) = (reftype $this, reftype $that);
$tthis and $tthat and $tthis eq $tthat or return undef;
# Check the children of the reference type
$self->{seen}->{$rthis} = $rthat;
my $method = "_equal_$tthat";
my $rv = $self->$method( $this, $that );
delete $self->{seen}->{$rthis};
$rv;
}
# Compare the children of two SCALAR references
sub _equal_SCALAR {
my ($self, $this, $that) = @_;
my ($cthis, $cthat) = ($$this, $$that);
return $self->_equal_blessed( $cthis, $cthat ) if blessed $cthis;
return $self->_equal_reference( $cthis, $cthat ) if ref $cthis;
return (defined $cthat and $cthis eq $cthat) if defined $cthis;
! defined $cthat;
}
# For completeness sake, lets just treat REF as a specialist SCALAR case
sub _equal_REF { shift->_equal_SCALAR(@_) }
# Compare the children of two ARRAY references
sub _equal_ARRAY {
my ($self, $this, $that) = @_;
# Compare the number of elements
scalar(@$this) == scalar(@$that) or return '';
# Check each element in the array.
# Descend depth-first.
foreach my $i ( 0 .. scalar(@$this) ) {
my ($cthis, $cthat) = ($this->[$i], $that->[$i]);
if ( blessed $cthis ) {
return '' unless $self->_equal_blessed( $cthis, $cthat );
} elsif ( ref $cthis ) {
return '' unless $self->_equal_reference( $cthis, $cthat );
} elsif ( defined $cthis ) {
return '' unless (defined $cthat and $cthis eq $cthat);
} else {
return '' if defined $cthat;
}
}
1;
}
# Compare the children of a HASH reference
sub _equal_HASH {
my ($self, $this, $that) = @_;
# Compare the number of keys
return '' unless scalar(keys %$this) == scalar(keys %$that);
# Compare each key, descending depth-first.
foreach my $k ( keys %$this ) {
return '' unless exists $that->{$k};
my ($cthis, $cthat) = ($this->{$k}, $that->{$k});
if ( blessed $cthis ) {
return '' unless $self->_equal_blessed( $cthis, $cthat );
} elsif ( ref $cthis ) {
return '' unless $self->_equal_reference( $cthis, $cthat );
} elsif ( defined $cthis ) {
return '' unless (defined $cthat and $cthis eq $cthat);
} else {
return '' if defined $cthat;
}
}
1;
}
# We do not support GLOB comparisons
sub _equal_GLOB {
my ($self, $this, $that) = @_;
warn('GLOB comparisons are not supported');
'';
}
# We do not support CODE comparisons
sub _equal_CODE {
my ($self, $this, $that) = @_;
refaddr $this == refaddr $that;
}
# We don't support IO comparisons
sub _equal_IO {
my ($self, $this, $that) = @_;
warn('IO comparisons are not supported');
'';
}
sub DESTROY {
# Take the screw up Document with us
if ( $_[0]->{Document} ) {
$_[0]->{Document}->DESTROY;
delete $_[0]->{Document};
}
}
1;
=pod
=head1 SUPPORT
See the L<support section|PPI/SUPPORT> in the main module.
=head1 AUTHOR
Adam Kennedy E<lt>adamk@cpan.orgE<gt>
=head1 COPYRIGHT
Copyright 2005 - 2011 Adam Kennedy.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|