PPI::Normal.3pm

Langue: en

Version: 2008-05-14 (debian - 07/07/09)

Section: 3 (Bibliothèques de fonctions)

NAME

PPI::Normal - Normalize Perl Documents

DESCRIPTION

Perl Documents, as created by PPI, are typically filled with all sorts of mess such as whitespace and comments and other things that don't effect the actual meaning of the code.

In addition, because there is more than one way to do most things, and the syntax of Perl itself is quite flexible, there are many ways in which the ``same'' code can look quite different.

PPI::Normal attempts to resolve this by providing a variety of mechanisms and algorithms to ``normalize'' Perl Documents, and determine a sort of base form for them (although this base form will be a memory structure, and not something that can be turned back into Perl source code).

The process itself is quite complex, and so for convenience and extensibility it has been separated into a number of layers. At a later point, it will be possible to write Plugin classes to insert additional normalization steps into the various different layers.

In addition, you can choose to do the normalization only as deep as a particular layer, depending on aggressively you want the normalization process to be.

METHODS


register $function => $layer, ...


register $function => $layer, ...

The "register" method is used by normalization method providers to tell the normalization engines which functions need to be run, and in which layer they apply.

Provide a set of key/value pairs, where the key is the full name of the function (in string form), and the value is the layer (see description of the layers above) in which it should be run.

Returns true if all functions are registered, or "undef" on error.

new

   my $level_1 = PPI::Normal->new();
   my $level_2 = PPI::Normal->new(2);
 
 

Creates a new normalization object, to which Document objects can be passed to be normalized.

Of course, what you probably REALLY want is just to call PPI::Document's "normalize" method.

Takes an optional single parameter of the normalisation layer to use, which at this time can be either ``1'' or ``2''.

Returns a new "PPI::Normal" object, or "undef" on error.

# Check we actually set the layer at creation my $layer_1 = PPI::Normal->new(); isa_ok( $layer_1, 'PPI::Normal' ); is( $layer_1->layer, 1, '->new() creates a layer 1' ); my $layer_1a = PPI::Normal->new(1); isa_ok( $layer_1a, 'PPI::Normal' ); is( $layer_1a->layer, 1, '->new(1) creates a layer 1' ); my $layer_2 = PPI::Normal->new(2); isa_ok( $layer_2, 'PPI::Normal' ); is( $layer_2->layer, 2, '->new(2) creates a layer 2' );

# Test bad things is( PPI::Normal->new(3), undef, '->new only allows up to layer 2' ); is( PPI::Normal->new(undef), undef, '->new(evil) returns undef' ); is( PPI::Normal->new(``foo''), undef, '->new(evil) returns undef' ); is( PPI::Normal->new(\``foo''), undef, '->new(evil) returns undef' ); is( PPI::Normal->new([]), undef, '->new(evil) returns undef' ); is( PPI::Normal->new({}), undef, '->new(evil) returns undef' );

layer

The "layer" accessor returns the normalisation layer of the object.

process

The "process" method takes anything that can be converted to a PPI::Document (object, SCALAR ref, filename), loads it and applies the normalisation process to the document.

Returns a PPI::Document::Normalized object, or "undef" on error.

my $doc1 = PPI::Document->new(\'print ``Hello World!\n'';'); isa_ok( $doc1, 'PPI::Document' ); my $doc2 = \'print ``Hello World!\n'';'; my $doc3 = \' print ``Hello World!\n''; # comment'; my $doc4 = \'print ``Hello World!\n''';

# Normalize them at level 1 my $layer1 = PPI::Normal->new(1); isa_ok( $layer1, 'PPI::Normal' ); my $nor11 = $layer1->process($doc1->clone); my $nor12 = $layer1->process($doc2); my $nor13 = $layer1->process($doc3); isa_ok( $nor11, 'PPI::Document::Normalized' ); isa_ok( $nor12, 'PPI::Document::Normalized' ); isa_ok( $nor13, 'PPI::Document::Normalized' );

# The first 3 should be the same, the second not is_deeply( { %$nor11 }, { %$nor12 }, 'Layer 1: 1 and 2 match' ); is_deeply( { %$nor11 }, { %$nor13 }, 'Layer 1: 1 and 3 match' );

# Normalize them at level 2 my $layer2 = PPI::Normal->new(2); isa_ok( $layer2, 'PPI::Normal' ); my $nor21 = $layer2->process($doc1); my $nor22 = $layer2->process($doc2); my $nor23 = $layer2->process($doc3); my $nor24 = $layer2->process($doc4); isa_ok( $nor21, 'PPI::Document::Normalized' ); isa_ok( $nor22, 'PPI::Document::Normalized' ); isa_ok( $nor23, 'PPI::Document::Normalized' ); isa_ok( $nor24, 'PPI::Document::Normalized' );

# The first 3 should be the same, the second not is_deeply( { %$nor21 }, { %$nor22 }, 'Layer 2: 1 and 2 match' ); is_deeply( { %$nor21 }, { %$nor23 }, 'Layer 2: 1 and 3 match' ); is_deeply( { %$nor21 }, { %$nor24 }, 'Layer 2: 1 and 4 match' );

NOTES

The following normalisation layers are implemented. When writing plugins, you should register each transformation function with the appropriate layer.

Layer 1 - Insignificant Data Removal

The basic step common to all normalization, layer 1 scans through the Document and removes all whitespace, comments, POD, and anything else that returns false for its "significant" method.

It also checks each Element and removes known-useless sub-element metadata such as the Element's physical position in the file.

Layer 2 - Significant Element Removal

After the removal of the insignificant data, Layer 2 removed larger, more complex, and superficially ``significant'' elements, that can be removed for the purposes of normalisation.

Examples from this layer include pragmas, now-useless statement separators (since the PDOM tree is holding statement elements), and several other minor bits and pieces.

Layer 3 - TO BE COMPLETED

This version of the forward-port of the Perl::Compare functionality to the 0.900+ API of PPI only implements Layer 1 and 2 at this time.

TO DO

- Write the other 4-5 layers :)

SUPPORT

See the support section in the main module.

AUTHOR

Adam Kennedy <adamk@cpan.org> Copyright (c) 2005 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.

POD ERRORS

Hey! The above document had some coding errors, which are explained below:
Around line 134:
'=begin' only takes one parameter, not several as in '=begin testing new after PPI::Document 12'
Around line 155:
=end testing without matching =begin. (Stack: [empty])
Around line 200:
'=begin' only takes one parameter, not several as in '=begin testing process after new 15'
Around line 239:
=end testing without matching =begin. (Stack: [empty])