| 1 | package PPIx::EditorTools::ReturnObject; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 25 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Simple object to return values from PPIx::EditorTools | ||||||||||||||||||||||||||||||||||||||||||
| 4 | 25 | $PPIx::EditorTools::ReturnObject::VERSION = '0.21'; | |||||||||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 26 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 26 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 26 | ▶ | return bless {@_}, ref($class) || $class; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | sub element { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
element: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 14 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # If element is a code ref, run the code once then cache the | ||||||||||||||||||||||||||||||||||||||||||
| 20 | # result | ||||||||||||||||||||||||||||||||||||||||||
| 21 | 14 | ▶ | if ( exists $self->{element} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
exists $self->{"element"} and ref $self->{"element"} and ref $self->{"element"} eq "CODE"
exists $self->{"element"} and ref $self->{"element"}
MC/DC33%1/3
exists $self->{"element"} and ref $self->{"element"} and ref $self->{"element"} eq "CODE"
exists $self->{"element"}
ref $self->{"element"}
ref $self->{"element"} eq "CODE"
Truth table50%2/4
A and B and C
A= exists $self->{"element"}B= ref $self->{"element"}C= ref $self->{"element"} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | and ref( $self->{element} ) | ||||||||||||||||||||||||||||||||||||||||||
| 23 | and ref( $self->{element} ) eq 'CODE' ) | ||||||||||||||||||||||||||||||||||||||||||
| 24 | { | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | $self->{element} = $self->{element}->(@_); | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 14 | return $self->{element}; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | ▶ | sub ppi { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ppi: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # $self->{ppi} = $doc if $doc; # TODO: and isa? | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | return $self->{ppi} if $self->{ppi}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | if ( $self->{code} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | my $code = $self->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | $self->{ppi} = PPI::Document->new( \$code ); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | return $self->{ppi}; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 20 | ▶ | sub code { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
code: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 20 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | # $self->{code} = $doc if $doc; | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 20 | ▶ | return $self->{code} if $self->{code}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 16 | ▶ | if ( $self->{ppi} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 16 | $self->{code} = $self->{ppi}->serialize; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 16 | return $self->{code}; | |||||||||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | PPIx::EditorTools::ReturnObject - Simple object to return values from PPIx::EditorTools | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | version 0.21 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | my $brace = PPIx::EditorTools::FindUnmatchedBrace->new->find( | ||||||||||||||||||||||||||||||||||||||||||
| 79 | code => "package TestPackage;\nsub x { 1;\n" | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | my $location = $brace->element->location; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | my $ppi = $brace->element->ppi; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | Retuning a simple C<PPI::Element> from many of the C<PPIx::EditorTools> | ||||||||||||||||||||||||||||||||||||||||||
| 87 | methods often results in the loss of the overall context for that element. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | C<PPIx::EditorTools::ReturnObject> provides an object that can be passed | ||||||||||||||||||||||||||||||||||||||||||
| 89 | around which retains the overall context. | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | For example, in C<PPIx::EditorTools::FindUnmatchedBrace> if the unmatched | ||||||||||||||||||||||||||||||||||||||||||
| 92 | brace were returned by its C<PPI::Structure::Block> the containing | ||||||||||||||||||||||||||||||||||||||||||
| 93 | C<PPI::Document> is likely to go out of scope, thus the C<location> | ||||||||||||||||||||||||||||||||||||||||||
| 94 | method no longer returns a valid location (rather it returns undef). | ||||||||||||||||||||||||||||||||||||||||||
| 95 | Using the C<ReturnObject> preserves the C<PPI::Document> and the containing | ||||||||||||||||||||||||||||||||||||||||||
| 96 | context. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =item new() | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | Constructor which should be used by C<PPIx::EditorTools>. Accepts the following | ||||||||||||||||||||||||||||||||||||||||||
| 105 | named parameters: | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | =item ppi | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | A C<PPI::Document> representing the (possibly modified) code. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =item code | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | A string representing the (possibly modified) code. | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =item element | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | A C<PPI::Element> or a subclass thereof representing the interesting element. | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | =item ppi | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | Accessor to retrieve the C<PPI::Document>. May create the C<PPI::Document> | ||||||||||||||||||||||||||||||||||||||||||
| 126 | from the $code string (lazily) if needed. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | =item code | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | Accessor to retrieve the string representation of the code. May be retrieved | ||||||||||||||||||||||||||||||||||||||||||
| 131 | from the C<PPI::Document> via the serialize method (lazily) if needed. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | C<PPIx::EditorTools>, L<App::EditorTools>, L<Padre>, and L<PPI>. | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | Steffen Mueller C<smueller@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | Mark Grimes C<mgrimes@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Ahmad M. Zawawi <ahmad.zawawi@gmail.com> | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Gabor Szabo <gabor@szabgab.com> | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =item * | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm.. | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 170 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||||||||||||||||