blib/lib/Pod/POM/Node.pm

filter: Statementstmt 97.1% 169 / 174 Branchbran 81.8% 54 / 66 Conditioncond 69.5% 16 / 23 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 7 totaltotal 87.1% 265 / 304 CC 45 SCAR 18.7
CC
45
Cov
91%
dump
12.0
add
10.0
_dump_text
7.3
CRAP 6.5
« blib/lib/Pod/POM/View/Pod.pm ↑ Summary blib/lib/Pod/POM/View.pm »
1 #============================================================= -*-Perl-*-
2 #
3 # Pod::POM::Node
4 #
5 # DESCRIPTION
6 # Base class for a node in a Pod::POM tree.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@wardley.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2000-2003 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 # REVISION
18 # $Id: Node.pm 91 2013-12-31 07:36:02Z ford $
19 #
20 #========================================================================
21  
22 package Pod::POM::Node;
23 18 $Pod::POM::Node::VERSION = '2.01';
24 18 require 5.006;
25  
26 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 18 ▶use Pod::POM::Nodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 18 ▶use Pod::POM::Constants qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 18 ▶use vars qw( $DEBUG $ERROR $NODES $NAMES $AUTOLOAD );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 18 ▶use constant DUMP_LINE_LENGTH => 80;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 18 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Pod::POM::Node::DEBUG 18 0 18
35 18 $NODES = {
36 pod => 'Pod::POM::Node::Pod',
37 head1 => 'Pod::POM::Node::Head1',
38 head2 => 'Pod::POM::Node::Head2',
39 head3 => 'Pod::POM::Node::Head3',
40 head4 => 'Pod::POM::Node::Head4',
41 over => 'Pod::POM::Node::Over',
42 item => 'Pod::POM::Node::Item',
43 begin => 'Pod::POM::Node::Begin',
44 for => 'Pod::POM::Node::For',
45 text => 'Pod::POM::Node::Text',
46 code => 'Pod::POM::Node::Code',
47 verbatim => 'Pod::POM::Node::Verbatim',
48 };
49 $NAMES = {
50 18 map { ( $NODES->{ $_ } => $_ ) } keys %$NODES,
51 };
52  
53 # overload stringification to present node via a view
54 use overload
55 18 ▶ '""' => 'present',
Subroutine BEGIN: called CC=1 SCAR=0.0
56 fallback => 1,
57 18 ▶ 'bool' => sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
58  
59 # alias meta() to metadata()
60 18 *meta = \*metadata;
61  
62  
63 #------------------------------------------------------------------------
64 # new($pom, @attr)
65 #
66 # Constructor method. Returns a new Pod::POM::Node::* object or undef
67 # on error. First argument is the Pod::POM parser object, remaining
68 # arguments are node attributes as specified in @ATTRIBS in derived class
69 # package.
70 #------------------------------------------------------------------------
71  
72 810 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod undocumented
73 810 my $class = shift;
74 810 my $pom = shift;
75 810 my ($type, @attribs, $accept);
76  
77 810 $type = $NAMES->{ $class };
78  
79 {
80 18 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
81 810 @attribs = @{"$class\::ATTRIBS"};
82 810 ▶ $accept = \@{"$class\::ACCEPT"} || [ ];
Condition50%1/2
\@{"${class}::ACCEPT";} || []
l!l
8100
MC/DC0%0/1
\@{"${class}::ACCEPT";} || []
\@{"${class}::ACCEPT";}
Truth table50%1/2
A
A= \@{"${class}::ACCEPT";}
Aresult
11
00
83 810 ▶ unless (%{"$class\::ACCEPT"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{"${class}::ACCEPT";}) 482 328 810
84 482 %{"$class\::ACCEPT"} = (
85 482 map { ( $_ => $NODES->{ $_ } ) } @$accept,
86 );
87 }
88 }
89  
90 # create object with slots for each acceptable child and overall content
91 my $self = bless {
92 type => $type,
93 content => bless([ ], 'Pod::POM::Node::Content'),
94 810 map { ($_ => bless([ ], 'Pod::POM::Node::Content')) }
95 (@$accept, 'code'),
96 }, $class;
97  
98 # set attributes from arguments
99 810 ▶ while(my ($key, $default) = splice(@attribs, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@attribs, 0, 2)) 755 807 1562
100 755 ▶ my $value = shift || $default;
Condition100%3/3
shift() || $default
l!l&&r!l&&!r
74663
MC/DC100%2/2
shift() || $default
shift() $default
Truth table100%3/3
A || B
A= shift()B= $default
ABresult
1X1
011
000
101 755 ▶ return $class->error("$type expected a $key")
Branch: 1 branch
BranchTrueFalseTotal
unless $value 3 752 755
102 unless $value;
103 752 $self->{ $key } = $value;
104 }
105  
106 807 return $self;
107 }
108  
109  
110 #------------------------------------------------------------------------
111 # add($pom, $type, @attr)
112 #
113 # Adds a new node as a child element (content) of the current node.
114 # First argument is the Pod::POM parser object. Second argument is the
115 # child node type specified by name (e.g. 'head1') which is mapped via
116 # the $NODES hash to a class name against which new() can be called.
117 # Remaining arguments are attributes passed to the child node constructor.
118 # Returns a reference to the new node (child was accepted) or one of the
119 # constants REDUCE (child terminated node, e.g. '=back' terminates an
120 # '=over' node), REJECT (child rejected, e.g. '=back' expected to terminate
121 # '=over' but something else found instead) or IGNORE (node didn't expect
122 # child and is implicitly terminated).
123 #------------------------------------------------------------------------
124  
125 1073 ▶sub add {
Subroutine add: called CC=10 SCAR=23.1
Pod undocumented
126 1073 my $self = shift;
127 1073 my $pom = shift;
128 1073 my $type = shift;
129 1073 my $class = ref $self;
130 1073 my ($name, $attribs, $accept, $expect, $nodeclass, $node);
131  
132 1073 ▶ $name = $NAMES->{ $class }
Condition50%1/2
$Pod::POM::Node::NAMES->{$class} || (return $self->error("no name for $class"))
l!l
10730
MC/DC0%0/1
$Pod::POM::Node::NAMES->{$class} || (return $self->error("no name for $class"))
$Pod::POM::Node::NAMES->{$class}
Truth table50%1/2
A
A= $Pod::POM::Node::NAMES->{$class}
Aresult
11
00
133 || return $self->error("no name for $class");
134 {
135 18 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
136 1073 $accept = \%{"$class\::ACCEPT"};
137 1073 $expect = ${"$class\::EXPECT"};
138 }
139  
140 # SHIFT: accept indicates child nodes that can be accepted; a
141 # new node is created, added it to content list and node specific
142 # list, then returned by reference.
143  
144 1073 ▶ if ($nodeclass = $accept->{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodeclass = $accept->{$type}) 752 321 1073
145 752 ▶ defined($node = $nodeclass->new($pom, @_))
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node 752 0 752
Condition60%3/5
defined $node || (defined($node = $nodeclass->new($pom, @_)) || (return $self->error($nodeclass->error)))
l!l&&r!l&&!r
07520
defined($node = $nodeclass->new($pom, @_)) or return $self->error($nodeclass->error)
l!l
7493
MC/DC0%0/2
defined $node || (defined($node = $nodeclass->new($pom, @_)) || (return $self->error($nodeclass->error)))
defined $node defined($node = $nodeclass->new($pom, @_))
Truth table33%1/3
A || B
A= defined $nodeB= defined($node = $nodeclass->new($pom, @_))
ABresult
1X1
011
000
146 || return $self->error($nodeclass->error())
147 unless defined $node;
148 749 push(@{ $self->{ $type } }, $node);
149 749 push(@{ $self->{ content } }, $node);
150 749 ▶ $pom->{in_begin} = 1 if $nodeclass eq 'Pod::POM::Node::Begin';
Branch: 1 branch
BranchTrueFalseTotal
if $nodeclass eq "Pod::POM::Node::Begin" 15 734 749
151 749 return $node;
152 }
153  
154 # REDUCE: expect indicates the token that should terminate this node
155 321 ▶ if (defined $expect && ($type eq $expect)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expect and $type eq $expect) 54 267 321
Condition100%3/3
defined $expect and $type eq $expect
!ll&&!rl&&r
264354
MC/DC100%2/2
defined $expect and $type eq $expect
defined $expect $type eq $expect
Truth table100%3/3
A and B
A= defined $expectB= $type eq $expect
ABresult
0X0
100
111
156 54 DEBUG("$name terminated by expected $type\n");
157 54 ▶ $pom->{in_begin} = 0 if $name eq 'begin';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "begin" 15 39 54
158 54 return REDUCE;
159 }
160  
161 # REJECT: expected terminating node was not found
162 267 ▶ if (defined $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expect) 3 264 267
163 3 DEBUG("$name rejecting $type, expecting a terminating $expect\n");
164 3 $self->error("$name expected a terminating $expect");
165 3 return REJECT;
166 }
167  
168 # IGNORE: don't know anything about this node
169 264 DEBUG("$name ignoring $type\n");
170 264 return IGNORE;
171 }
172  
173  
174 #------------------------------------------------------------------------
175 # present($view)
176 #
177 # Present the node by making a callback on the appropriate method against
178 # the view object passed as an argument. $Pod::POM::DEFAULT_VIEW is used
179 # if $view is unspecified.
180 #------------------------------------------------------------------------
181  
182 304 ▶sub present {
Subroutine present: called CC=3 SCAR=11.0
Pod undocumented
183 304 my ($self, $view, @args) = @_;
184 304 ▶ $view ||= $Pod::POM::DEFAULT_VIEW;
Condition66%2/3
$view ||= $Pod::POM::DEFAULT_VIEW
l!l&&r!l&&!r
29680
MC/DC0%0/2
$view ||= $Pod::POM::DEFAULT_VIEW
$view $Pod::POM::DEFAULT_VIEW
Truth table66%2/3
A ||= B
A= $viewB= $Pod::POM::DEFAULT_VIEW
ABresult
1X1
011
000
185 304 my $type = $self->{ type };
186 304 my $method = "view_$type";
187 304 DEBUG("presenting method $method to $view\n");
188 304 my $txt = $view->$method($self, @args);
189 304 ▶ if ($view->can("encode")){
Branch: 1 branch
BranchTrueFalseTotal
if ($view->can('encode')) { } 113 191 304
190 113 return $view->encode($txt);
191 } else {
192 191 return $txt;
193 }
194 }
195  
196  
197 #------------------------------------------------------------------------
198 # metadata()
199 # metadata($key)
200 # metadata($key, $value)
201 #
202 # Returns the metadata hash when called without any arguments. Returns
203 # the value of a metadata item when called with a single argument.
204 # Sets a metadata item to a value when called with two arguments.
205 #------------------------------------------------------------------------
206  
207 5 ▶sub metadata {
Subroutine metadata: called CC=5 SCAR=16.1
Pod undocumented
208 5 my ($self, $key, $value) = @_;
209 5 ▶ my $metadata = $self->{ METADATA } ||= { };
Condition100%2/2
$self->{"METADATA"} ||= {}
l!l
41
MC/DC100%1/1
$self->{"METADATA"} ||= {}
$self->{"METADATA"}
Truth table100%2/2
A
A= $self->{"METADATA"}
Aresult
11
00
210  
211 5 ▶ return $metadata unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 4 5
212  
213 4 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 2 2 4
214 2 $metadata->{ $key } = $value;
215 }
216 else {
217 2 $value = $self->{ METADATA }->{ $key };
218 2 ▶ return defined $value ? $value
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 0 2
219 : $self->error("no such metadata item: $key");
220 }
221 }
222  
223  
224 #------------------------------------------------------------------------
225 # error()
226 # error($msg, ...)
227 #
228 # May be called as a class or object method to set or retrieve the
229 # package variable $ERROR (class method) or internal member
230 # $self->{ _ERROR } (object method). The presence of parameters indicates
231 # that the error value should be set. Undef is then returned. In the
232 # absence of parameters, the current error value is returned.
233 #------------------------------------------------------------------------
234  
235 18 ▶sub error {
Subroutine error: called CC=4 SCAR=13.9
Pod undocumented
236 18 my $self = shift;
237 18 my $errvar;
238 # use Carp;
239  
240 {
241 18 ▶ no strict qw( refs );
Subroutine BEGIN: called CC=1 SCAR=0.0
242 18 ▶ if (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 12 6 18
243 # my ($pkg, $file, $line) = caller();
244 # print STDERR "called from $file line $line\n";
245 # croak "cannot get/set error in non-hash: $self\n"
246 # unless UNIVERSAL::isa($self, 'HASH');
247 12 $errvar = \$self->{ ERROR };
248 }
249 else {
250 6 $errvar = \${"$self\::ERROR"};
251 }
252 }
253 18 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 9 9 18
254 9 ▶ $$errvar = ref($_[0]) ? shift : join('', @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 9 9
255 9 return undef;
256 }
257 else {
258 9 return $$errvar;
259 }
260 }
261  
262  
263 #------------------------------------------------------------------------
264 # dump()
265 #
266 # Returns a representation of the element and all its children in a
267 # format useful only for debugging. The structure of the document is
268 # shown by indentation (inspired by HTML::Element).
269 #------------------------------------------------------------------------
270  
271 248 ▶sub dump {
Subroutine dump: called CC=12 SCAR=24.9
Pod undocumented
272 248 my($self, $depth) = @_;
273 248 my $output;
274 248 ▶ $depth = 0 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 10 238 248
275 248 my $nodepkg = ref $self;
276 248 ▶ if ($self->isa('REF')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->isa('REF')) { } 125 123 248
277 125 $self = $$self;
278 125 my $cmd = $self->[CMD];
279 125 my @content = @{ $self->[CONTENT] };
280 125 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) 27 98 125
281 27 $output .= (" " x $depth) . $cmd . $self->[LPAREN] . "\n";
282 }
283 125 foreach my $item (@content) {
284 173 ▶ if (ref $item) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 27 146 173
285 27 $output .= $item->dump($depth+1); # recurse
286 }
287 else { # text node
288 146 $output .= _dump_text($item, $depth+1);
289 }
290 }
291 125 ▶ if ($cmd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd) 27 98 125
292 27 $output .= (" " x $depth) . $self->[RPAREN] . "\n", ;
293 }
294 }
295 else {
296 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
297 123 my @attrs = sort keys %{{ @{"${nodepkg}::ATTRIBS"} }};
298 123 $output .= (" " x $depth) . $self->type . "\n";
299 123 foreach my $attr (@attrs) {
300 113 ▶ if (my $value = $self->{$attr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $self->{$attr}) 113 0 113
301 113 $output .= (" " x ($depth+1)) . "\@$attr\n";
302
303 113 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 98 15 113
304 98 $output .= $value->dump($depth+1);
305 }
306 else {
307 15 $output .= _dump_text($value, $depth+2);
308 }
309 }
310 }
311 123 foreach my $item (@{$self->{content}}) {
312 113 ▶ if (ref $item) { # element
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item) { } 113 0 113
313 113 $output .= $item->dump($depth+1); # recurse
314 }
315 else { # text node
316 0 $output .= _dump_text($item, $depth+1);
317 }
318 }
319 }
320  
321 248 return $output;
322 }
323  
324 161 ▶sub _dump_text {
Subroutine _dump_text: called CC=5 SCAR=19.8
325 161 my ($text, $depth) = @_;
326  
327 161 my $output = "";
328 161 my $padding = " " x $depth;
329 161 my $max_text_len = DUMP_LINE_LENGTH - length($depth) - 2;
330  
331 161 foreach my $line (split(/\n/, $text)) {
332 172 $output .= $padding;
333 172 ▶ if (length($line) > $max_text_len or $line =~ m<[\x00-\x1F]>) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $line > $max_text_len or $line =~ /[\x00-\x1F]/) { } 0 172 172
Condition33%1/3
length $line > $max_text_len or $line =~ /[\x00-\x1F]/
l!l&&r!l&&!r
00172
MC/DC0%0/2
length $line > $max_text_len or $line =~ /[\x00-\x1F]/
length $line > $max_text_len $line =~ /[\x00-\x1F]/
Truth table33%1/3
A or B
A= length $line > $max_text_lenB= $line =~ /[\x00-\x1F]/
ABresult
1X1
011
000
334 # it needs prettyin' up somehow or other
335 0 ▶ my $x = (length($line) <= $max_text_len) ? $_ : (substr($line, 0, $max_text_len) . '...');
Branch: 1 branch
BranchTrueFalseTotal
length $line <= $max_text_len ? : 0 0 0
336 0 $x =~ s<([\x00-\x1F])>
337 0 <'\\x'.(unpack("H2",$1))>eg;
338 0 $output .= qq{"$x"\n};
339 } else {
340 172 $output .= qq{"$line"\n};
341 }
342 }
343 161 return $output;
344 }
345  
346  
347 #------------------------------------------------------------------------
348 # AUTOLOAD
349 #------------------------------------------------------------------------
350  
351 7217 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=18.0
352 7217 my $self = shift;
353 7217 my $name = $AUTOLOAD;
354 7217 my $item;
355  
356 7217 $name =~ s/.*:://;
357 7217 ▶ return if $name eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "DESTROY" 6490 727 7217
358  
359 # my ($pkg, $file, $line) = caller();
360 # print STDERR "called from $file line $line to return ", ref($item), "\n";
361  
362 727 ▶ return $self->error("can't manipulate \$self")
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($self, "HASH") 0 727 727
363 unless UNIVERSAL::isa($self, 'HASH');
364 return $self->error("no such member: $name")
365 727 ▶ unless defined ($item = $self->{ $name });
Branch: 1 branch
BranchTrueFalseTotal
unless defined($item = $self->{$name}) 0 727 727
366  
367 727 ▶ return wantarray ? ( UNIVERSAL::isa($item, 'ARRAY') ? @$item : $item )
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 14 713 727
UNIVERSAL::isa($item, 'ARRAY') ? : 1 13 14
368 : $item;
369 }
370  
371  
372 #------------------------------------------------------------------------
373 # DEBUG(@msg)
374 #------------------------------------------------------------------------
375  
376 625 ▶sub DEBUG {
Subroutine DEBUG: called CC=2 SCAR=7.6
Pod undocumented
377 625 ▶ print STDERR "DEBUG: ", @_ if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Pod::POM::Node::DEBUG 0 625 625
378 }
379  
380 18 1;
381  
382  
383  
384 =head1 NAME
385  
386 Pod::POM::Node - base class for a POM node
387  
388 =head1 SYNOPSIS
389  
390 package Pod::POM::Node::Over;
391 use parent qw( Pod::POM::Node );
392 use vars qw( @ATTRIBS @ACCEPT $EXPECT $ERROR );
393  
394 @ATTRIBS = ( indent => 4 );
395 @ACCEPT = qw( over item begin for text verbatim );
396 $EXPECT = q( back );
397  
398 package main;
399 my $list = Pod::POM::Node::Over->new(8);
400 $list->add('item', 'First Item');
401 $list->add('item', 'Second Item');
402 ...
403  
404 =head1 DESCRIPTION
405  
406 This documentation describes the inner workings of the Pod::POM::Node
407 module and gives a brief overview of the relationship between it and
408 its derived classes. It is intended more as a guide to the internals
409 for interested hackers than as general user documentation. See
410 L<Pod::POM> for information on using the modules.
411  
412 This module implements a base class node which is subclassed to
413 represent different elements within a Pod Object Model.
414  
415 package Pod::POM::Node::Over;
416 use parent qw( Pod::POM::Node );
417  
418 The base class implements the new() constructor method to instantiate
419 new node objects.
420  
421 my $list = Pod::POM::Node::Over->new();
422  
423 The characteristics of a node can be specified by defining certain
424 variables in the derived class package. The C<@ATTRIBS> list can be
425 used to denote attributes that the node should accept. In the case of
426 an C<=over> node, for example, an C<indent> attribute can be specified
427 which otherwise defaults to 4.
428  
429 package Pod::POM::Node::Over;
430 use parent qw( Pod::POM::Node );
431 use vars qw( @ATTRIBS $ERROR );
432  
433 @ATTRIBS = ( indent => 4 );
434  
435 The new() method will now expect an argument to set the indent value,
436 or will use 4 as the default if no argument is provided.
437  
438 my $list = Pod::POM::Node::Over->new(8); # indent: 8
439 my $list = Pod::POM::Node::Over->new( ); # indent: 4
440  
441 If the default value is undefined then the argument is mandatory.
442  
443 package Pod::POM::Node::Head1;
444 use parent qw( Pod::POM::Node );
445 use vars qw( @ATTRIBS $ERROR );
446  
447 @ATTRIBS = ( title => undef );
448  
449 package main;
450 my $head = Pod::POM::Node::Head1->new('My Title');
451  
452 If a mandatory argument isn't provided then the constructor will
453 return undef to indicate failure. The $ERROR variable in the derived
454 class package is set to contain a string of the form "$type expected a
455 $attribute".
456  
457 # dies with error: "head1 expected a title"
458 my $head = Pod::POM::Node::Head1->new()
459 || die $Pod::POM::Node::Head1::ERROR;
460  
461 For convenience, the error() subroutine can be called as a class
462 method to retrieve this value.
463  
464 my $type = 'Pod::POM::Node::Head1';
465 my $head = $type->new()
466 || die $type->error();
467  
468 The C<@ACCEPT> package variable can be used to indicate the node types
469 that are permitted as children of a node.
470  
471 package Pod::POM::Node::Head1;
472 use parent qw( Pod::POM::Node );
473 use vars qw( @ATTRIBS @ACCEPT $ERROR );
474  
475 @ATTRIBS = ( title => undef );
476 @ACCEPT = qw( head2 over begin for text verbatim );
477  
478 The add() method can then be called against a node to add a new child
479 node as part of its content.
480  
481 $head->add('over', 8);
482  
483 The first argument indicates the node type. The C<@ACCEPT> list is
484 examined to ensure that the child node type is acceptable for the
485 parent node. If valid, the constructor for the relevant child node
486 class is called passing any remaining arguments as attributes. The
487 new node is then returned.
488  
489 my $list = $head->add('over', 8);
490  
491 The error() method can be called against the I<parent> node to retrieve
492 any constructor error generated by the I<child> node.
493  
494 my $list = $head->add('over', 8);
495 die $head->error() unless defined $list;
496  
497 If the child node is not acceptable to the parent then the add()
498 method returns one of the constants IGNORE, REDUCE or REJECT, as
499 defined in Pod::POM::Constants. These return values are used by the
500 Pod::POM parser module to implement a simple shift/reduce parser.
501  
502 In the most common case, IGNORE is returned to indicate that the
503 parent node doesn't know anything about the new child node. The
504 parser uses this as an indication that it should back up through the
505 parse stack until it finds a node which I<will> accept this child node.
506 Through this mechanism, the parser is able to implicitly terminate
507 certain POD blocks. For example, a list item initiated by a C<=item>
508 tag will I<not> accept another C<=item> tag, but will instead return IGNORE.
509 The parser will back out until it finds the enclosing C<=over> node
510 which I<will> accept it. Thus, a new C<=item> implicitly terminates any
511 previous C<=item>.
512  
513 The C<$EXPECT> package variable can be used to indicate a node type
514 which a parent expects to terminate itself. An C<=over> node, for
515 example, should always be terminated by a matching C<=back>. When
516 such a match is made, the add() method returns REDUCE to indicate
517 successful termination.
518  
519 package Pod::POM::Node::Over;
520 use parent qw( Pod::POM::Node );
521 use vars qw( @ATTRIBS @ACCEPT $EXPECT $ERROR );
522  
523 @ATTRIBS = ( indent => 4 );
524 @ACCEPT = qw( over item begin for text verbatim );
525 $EXPECT = q( back );
526  
527 package main;
528 my $list = Pod::POM::Node::Over->new();
529 my $item = $list->add('item');
530 $list->add('back'); # returns REDUCE
531  
532 If a child node isn't specified in the C<@ACCEPT> list or doesn't match
533 any C<$EXPECT> specified then REJECT is returned. The parent node sets
534 an internal error of the form "$type expected a terminating $expect".
535 The parser uses this to detect missing POD tags. In nearly all cases
536 the parser is smart enough to fix the incorrect structure and downgrades
537 any errors to warnings.
538  
539 # dies with error 'over expected terminating back'
540 ref $list->add('head1', 'My Title') # returns REJECT
541 || die $list->error();
542  
543 Each node contains a 'type' field which contains a simple string
544 indicating the node type, e.g. 'head1', 'over', etc. The $NODES and
545 $NAMES package variables (in the base class) reference hash arrays
546 which map these names to and from package names (e.g. head1 E<lt>=E<gt>
547 Pod::POM::Node::Head1).
548  
549 print $list->{ type }; # 'over'
550  
551 An AUTOLOAD method is provided to access to such internal items for
552 those who don't like violating an object's encapsulation.
553  
554 print $list->type();
555  
556 Nodes also contain a 'content' list, blessed into the
557 Pod::POM::Node::Content class, which contains the content (child
558 elements) for the node. The AUTOLOAD method returns this as a list
559 reference or as a list of items depending on the context in which it
560 is called.
561  
562 my $items = $list->content();
563 my @items = $list->content();
564  
565 Each node also contains a content list for each individual child node
566 type that it may accept.
567  
568 my @items = $list->item();
569 my @text = $list->text();
570 my @vtext = $list->verbatim();
571  
572 The present() method is used to present a node through a particular view.
573 This simply maps the node type to a method which is then called against the
574 view object. This is known as 'double dispatch'.
575  
576 my $view = 'Pod::POM::View::HTML';
577 print $list->present($view);
578  
579 The method name is constructed from the node type prefixed by 'view_'.
580 Thus the following are roughly equivalent.
581  
582 $list->present($view);
583  
584 $view->view_list($list);
585  
586 The benefit of the former over the latter is, of course, that the
587 caller doesn't need to know or determine the type of the node. The
588 node itself is in the best position to determine what type it is.
589  
590 =head1 AUTHOR
591  
592 Andy Wardley E<lt>abw@kfs.orgE<gt>
593  
594 =head1 COPYRIGHT
595  
596 Copyright (C) 2000, 2001 Andy Wardley. All Rights Reserved.
597  
598 This module is free software; you can redistribute it and/or
599 modify it under the same terms as Perl itself.
600  
601 =head1 SEE ALSO
602  
603 Consult L<Pod::POM> for a general overview and examples of use.
604  
« blib/lib/Pod/POM/View/Pod.pm ↑ Summary blib/lib/Pod/POM/View.pm »