1MDOM::Element(3) User Contributed Perl Documentation MDOM::Element(3)
2
3
4
6 MDOM::Element - The abstract Element class, a base for all source
7 objects
8
10 MDOM::Element is the root of the PDOM tree
11
13 The abstract "MDOM::Element" serves as a base class for all source-
14 related objects, from a single whitespace token to an entire document.
15 It provides a basic set of methods to provide a common interface and
16 basic implementations.
17
19 significant
20 Because we treat whitespace and other non-code items as Tokens (in
21 order to be able to "round trip" the MDOM::Document back to a file) the
22 "significant" method allows us to distinguish between tokens that form
23 a part of the code, and tokens that aren't significant, such as
24 whitespace, POD, or the portion of a file after (and including) the
25 "__END__" token.
26
27 Returns true if the Element is significant, or false it not.
28
29 lineno
30 Accessor for current line number.
31
32 class
33 The "class" method is provided as a convenience, and really does
34 nothing more than returning "ref($self)". However, some people have
35 found that they appreciate the laziness of "$Foo->class eq 'whatever'",
36 so I have caved to popular demand and included it.
37
38 Returns the class of the Element as a string
39
40 tokens
41 The "tokens" method returns a list of MDOM::Token objects for the
42 Element, essentially getting back that part of the document as if it
43 had not been lexed.
44
45 This also means there are no Statements and no Structures in the list,
46 just the Token classes.
47
48 content
49 For any "MDOM::Element", the "content" method will reconstitute the
50 base code for it as a single string. This method is also the method
51 used for overloading stringification. When an Element is used in a
52 double-quoted string for example, this is the method that is called.
53
54 WARNING:
55
56 You should be aware that because of the way that here-docs are handled,
57 any here-doc content is not included in "content", and as such you
58 should not eval or execute the result if it contains any
59 MDOM::Token::HereDoc.
60
61 The MDOM::Document method "serialize" should be used to stringify a
62 PDOM document into something that can be executed as expected.
63
64 Returns the basic code as a string (excluding here-doc content).
65
66 parent
67 Elements themselves are not intended to contain other Elements, that is
68 left to the MDOM::Node abstract class, a subclass of "MDOM::Element".
69 However, all Elements can be contained within a parent Node.
70
71 If an Element is within a parent Node, the "parent" method returns the
72 Node.
73
74 statement
75 For a "MDOM::Element" that is contained (at some depth) within a
76 MDOM::Statment, the "statement" method will return the first parent
77 Statement object lexically 'above' the Element.
78
79 Returns a MDOM::Statement object, which may be the same Element if the
80 Element is itself a MDOM::Statement object.
81
82 Returns false if the Element is not within a Statement and is not
83 itself a Statement.
84
85 top
86 For a "MDOM::Element" that is contained within a PDOM tree, the "top"
87 method will return the top-level Node in the tree. Most of the time
88 this should be a MDOM::Document object, however this will not always be
89 so. For example, if a subroutine has been removed from its Document, to
90 be moved to another Document.
91
92 Returns the top-most PDOM object, which may be the same Element, if it
93 is not within any parent PDOM object.
94
95 document
96 For an Element that is contained within a MDOM::Document object, the
97 "document" method will return the top-level Document for the Element.
98
99 Returns the MDOM::Document for this Element, or false if the Element is
100 not contained within a Document.
101
102 next_sibling
103 All MDOM::Node objects (specifically, our parent Node) contain a number
104 of "MDOM::Element" objects. The "next_sibling" method returns the
105 "MDOM::Element" immediately after the current one, or false if there is
106 no next sibling.
107
108 snext_sibling
109 As per the other 's' methods, the "snext_sibling" method returns the
110 next significant sibling of the "MDOM::Element" object.
111
112 Returns a "MDOM::Element" object, or false if there is no 'next'
113 significant sibling.
114
115 previous_sibling
116 All MDOM::Node objects (specifically, our parent Node) contain a number
117 of "MDOM::Element" objects. The "previous_sibling" method returns the
118 Element immediately before the current one, or false if there is no
119 'previous' "MDOM::Element" object.
120
121 sprevious_sibling
122 As per the other 's' methods, the "sprevious_sibling" method returns
123 the previous significant sibling of the "MDOM::Element" object.
124
125 Returns a "MDOM::Element" object, or false if there is no 'previous'
126 significant sibling.
127
128 first_token
129 As a support method for higher-order algorithms that deal specifically
130 with tokens and actual Perl content, the "first_token" method finds the
131 first MDOM::Token object within or equal to this one.
132
133 That is, if called on a MDOM::Node subclass, it will descend until it
134 finds a MDOM::Token. If called on a MDOM::Token object, it will return
135 the same object.
136
137 Returns a MDOM::Token object, or dies on error (which should be
138 extremely rare and only occur if an illegal empty MDOM::Statement
139 exists below the current Element somewhere.
140
141 last_token
142 As a support method for higher-order algorithms that deal specifically
143 with tokens and actual Perl content, the "last_token" method finds the
144 last MDOM::Token object within or equal to this one.
145
146 That is, if called on a MDOM::Node subclass, it will descend until it
147 finds a MDOM::Token. If called on a MDOM::Token object, it will return
148 the itself.
149
150 Returns a MDOM::Token object, or dies on error (which should be
151 extremely rare and only occur if an illegal empty MDOM::Statement
152 exists below the current Element somewhere.
153
154 next_token
155 As a support method for higher-order algorithms that deal specifically
156 with tokens and actual Perl content, the "next_token" method finds the
157 MDOM::Token object that is immediately after the current Element, even
158 if it is not within the same parent MDOM::Node as the one for which the
159 method is being called.
160
161 Note that this is not defined as a MDOM::Token-specific method, because
162 it can be useful to find the next token that is after, say, a
163 MDOM::Statement, although obviously it would be useless to want the
164 next token after a MDOM::Document.
165
166 Returns a MDOM::Token object, or false if there are no more tokens
167 after the Element.
168
169 previous_token
170 As a support method for higher-order algorithms that deal specifically
171 with tokens and actual Perl content, the "previous_token" method finds
172 the MDOM::Token object that is immediately before the current Element,
173 even if it is not within the same parent MDOM::Node as this one.
174
175 Note that this is not defined as a MDOM::Token-only method, because it
176 can be useful to find the token is before, say, a MDOM::Statement,
177 although obviously it would be useless to want the next token before a
178 MDOM::Document.
179
180 Returns a MDOM::Token object, or false if there are no more tokens
181 before the "Element".
182
183 clone
184 As per the Clone module, the "clone" method makes a perfect copy of an
185 Element object. In the generic case, the implementation is done using
186 the Clone module's mechanism itself. In higher-order cases, such as for
187 Nodes, there is more work involved to keep the parent-child links
188 intact.
189
190 insert_before @Elements
191 The "insert_before" method allows you to insert lexical perl content,
192 in the form of "MDOM::Element" objects, before the calling "Element".
193 You need to be very careful when modifying perl code, as it's easy to
194 break things.
195
196 In its initial incarnation, this method allows you to insert a single
197 Element, and will perform some basic checking to prevent you inserting
198 something that would be structurally wrong (in PDOM terms).
199
200 In future, this method may be enhanced to allow the insertion of
201 multiple Elements, inline-parsed code strings or
202 MDOM::Document::Fragment objects.
203
204 Returns true if the Element was inserted, false if it can not be
205 inserted, or "undef" if you do not provide a MDOM::Element object as a
206 parameter.
207
208 insert_after @Elements
209 The "insert_after" method allows you to insert lexical perl content, in
210 the form of "MDOM::Element" objects, after the calling "Element". You
211 need to be very careful when modifying perl code, as it's easy to break
212 things.
213
214 In its initial incarnation, this method allows you to insert a single
215 Element, and will perform some basic checking to prevent you inserting
216 something that would be structurally wrong (in PDOM terms).
217
218 In future, this method may be enhanced to allow the insertion of
219 multiple Elements, inline-parsed code strings or
220 MDOM::Document::Fragment objects.
221
222 Returns true if the Element was inserted, false if it can not be
223 inserted, or "undef" if you do not provide a MDOM::Element object as a
224 parameter.
225
226 remove
227 For a given "MDOM::Element", the "remove" method will remove it from
228 its parent intact, along with all of its children.
229
230 Returns the "Element" itself as a convenience, or "undef" if an error
231 occurs while trying to remove the "Element".
232
233 delete
234 For a given "MDOM::Element", the "remove" method will remove it from
235 its parent, immediately deleting the "Element" and all of its children
236 (if it has any).
237
238 Returns true if the "Element" was successfully deleted, or "undef" if
239 an error occurs while trying to remove the "Element".
240
241 replace $Element
242 Although some higher level class support more exotic forms of replace,
243 at the basic level the "replace" method takes a single "Element" as an
244 argument and replaces the current "Element" with it.
245
246 To prevent accidental damage to code, in this initial implementation
247 the replacement element must be of the same class (or a subclass) as
248 the one being replaced.
249
250 location
251 If the Element exists within a MDOM::Document that has indexed the
252 Element locations using "MDOM::Document::index_locations", the
253 "location" method will return the location of the first character of
254 the Element within the Document.
255
256 Returns the location as a reference to a three-element array in the
257 form "[ $line, $rowchar, $col ]". The values are in a human format,
258 with the first character of the file located at "[ 1, 1, 1 ]".
259
260 The second and third numbers are similar, except that the second is the
261 literal horizontal character, and the third is the visual column,
262 taking into account tabbing.
263
264 Returns "undef" on error, or if the MDOM::Document object has not been
265 indexed.
266
268 It would be nice if "location" could be used in an ad-hoc manner. That
269 is, if called on an Element within a Document that has not been
270 indexed, it will do a one-off calculation to find the location. It
271 might be very painful if someone started using it a lot, without
272 remembering to index the document, but it would be handy for things
273 that are only likely to use it once, such as error handlers.
274
276 See the support section in the main module.
277
279 Adam Kennedy <adamk@cpan.org>
280
282 Copyright 2001 - 2006 Adam Kennedy.
283
284 This program is free software; you can redistribute it and/or modify it
285 under the same terms as Perl itself.
286
287 The full text of the license can be found in the LICENSE file included
288 with this module.
289
290
291
292perl v5.34.0 2022-01-21 MDOM::Element(3)