| PPIx-EditorTools documentation | Contained in the PPIx-EditorTools distribution. |
my $brace = PPIx::EditorTools::FindUnmatchedBrace->new->find(
code => "package TestPackage;\nsub x { 1;\n"
);
my $location = $brace->element->location;
my $ppi = $brace->element->ppi;
Retuning a simple PPI::Element from many of the PPIx::EditorTools
methods often results in the loss of the overall context for that element.
PPIx::EditorTools::ReturnObject provides an object that can be passed
around which retains the overall context.
For example, in PPIx::EditorTools::FindUnmatchedBrace if the unmatched
brace were returned by its PPI::Structure::Block the containing
PPI::Document is likely to go out of scope, thus the location
method no longer returns a valid location (rather it returns undef).
Using the ReturnObject preserves the PPI::Document and the containing
context.
Constructor which should be used by PPIx::EditorTools. Accepts the following
named parameters:
A PPI::Document representing the (possibly modified) code.
A string representing the (possibly modified) code.
A PPI::Element or a subclass thereof representing the interesting element.
Accessor to retrieve the PPI::Document. May create the PPI::Document
from the $code string (lazily) if needed.
Accessor to retrieve the string representation of the code. May be retrieved
from the PPI::Document via the serialize method (lazily) if needed.
PPIx::EditorTools, App::EditorTools, Padre, and PPI.
| PPIx-EditorTools documentation | Contained in the PPIx-EditorTools distribution. |
package PPIx::EditorTools::ReturnObject; # ABSTRACT: Simple object to return values from PPIx::EditorTools use 5.008; use strict; use warnings; use Carp; our $VERSION = '0.15';
sub new { my $class = shift; return bless {@_}, ref($class) || $class; } sub element { my ($self) = @_; # If element is a code ref, run the code once then cache the # result if ( exists $self->{element} and ref( $self->{element} ) and ref( $self->{element} ) eq 'CODE' ) { $self->{element} = $self->{element}->(@_); } return $self->{element}; } sub ppi { my ( $self, $doc ) = @_; # $self->{ppi} = $doc if $doc; # TODO: and isa? return $self->{ppi} if $self->{ppi}; if ( $self->{code} ) { my $code = $self->{code}; $self->{ppi} = PPI::Document->new( \$code ); return $self->{ppi}; } return; } sub code { my ( $self, $doc ) = @_; # $self->{code} = $doc if $doc; return $self->{code} if $self->{code}; if ( $self->{ppi} ) { $self->{code} = $self->{ppi}->serialize; return $self->{code}; } return; } 1; __END__
# Copyright 2008-2009 The Padre development team as listed in Padre.pm. # LICENSE # This program is free software; you can redistribute it and/or # modify it under the same terms as Perl 5 itself.