File Coverage

blib/lib/PPIx/EditorTools/ReturnObject.pm
Criterion Covered Total %
statement 28 30 93.3
branch 8 10 80.0
condition 4 9 44.4
subroutine 8 8 100.0
pod 4 4 100.0
total 52 61 85.2


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::ReturnObject;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Simple object to return values from PPIx::EditorTools
4             $PPIx::EditorTools::ReturnObject::VERSION = '0.21';
5 11     11   144 use 5.008;
  11         31  
6 11     11   46 use strict;
  11         16  
  11         179  
7 11     11   42 use warnings;
  11         18  
  11         385  
8 11     11   50 use Carp;
  11         19  
  11         3079  
9              
10              
11             sub new {
12 22     22 1 954 my $class = shift;
13 22   33     277 return bless {@_}, ref($class) || $class;
14             }
15              
16             sub element {
17 14     14 1 4187 my ($self) = @_;
18              
19             # If element is a code ref, run the code once then cache the
20             # result
21 14 100 33     98 if ( exists $self->{element}
      66        
22             and ref( $self->{element} )
23             and ref( $self->{element} ) eq 'CODE' )
24             {
25 2         6 $self->{element} = $self->{element}->(@_);
26             }
27              
28 14         129 return $self->{element};
29             }
30              
31             sub ppi {
32 3     3 1 7 my ( $self ) = @_;
33              
34             # $self->{ppi} = $doc if $doc; # TODO: and isa?
35              
36 3 100       12 return $self->{ppi} if $self->{ppi};
37              
38 2 50       12 if ( $self->{code} ) {
39 2         5 my $code = $self->{code};
40 2         8 $self->{ppi} = PPI::Document->new( \$code );
41 2         15051 return $self->{ppi};
42             }
43              
44 0         0 return;
45             }
46              
47             sub code {
48 16     16 1 1154 my ( $self ) = @_;
49              
50             # $self->{code} = $doc if $doc;
51              
52 16 100       61 return $self->{code} if $self->{code};
53              
54 12 50       40 if ( $self->{ppi} ) {
55 12         43 $self->{code} = $self->{ppi}->serialize;
56 12         7431 return $self->{code};
57             }
58              
59 0           return;
60             }
61              
62             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 from many of the C
87             methods often results in the loss of the overall context for that element.
88             C provides an object that can be passed
89             around which retains the overall context.
90              
91             For example, in C if the unmatched
92             brace were returned by its C the containing
93             C is likely to go out of scope, thus the C
94             method no longer returns a valid location (rather it returns undef).
95             Using the C preserves the C 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. Accepts the following
105             named parameters:
106              
107             =over 4
108              
109             =item ppi
110              
111             A C 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 or a subclass thereof representing the interesting element.
120              
121             =back
122              
123             =item ppi
124              
125             Accessor to retrieve the C. May create the C
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 via the serialize method (lazily) if needed.
132              
133             =back
134              
135             =head1 SEE ALSO
136              
137             C, L, L, and L.
138              
139             =head1 AUTHORS
140              
141             =over 4
142              
143             =item *
144              
145             Steffen Mueller C
146              
147             =item *
148              
149             Mark Grimes C
150              
151             =item *
152              
153             Ahmad M. Zawawi
154              
155             =item *
156              
157             Gabor Szabo
158              
159             =item *
160              
161             Yanick Champoux
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__