Skip to content

Commit 47ac443

Browse files
author
Chris White
committed
L1 on{} default now BYE; added tests of L1
L1 now defaults to BYE for perform{} or on{} actions. This is because any text content the node may have is not available at HI, which I think violates the Principle of Least Surprise.
1 parent efa6206 commit 47ac443

File tree

5 files changed

+332
-28
lines changed

5 files changed

+332
-28
lines changed

L1.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ XML::Axk::Core::L1 - ack-like XML processor, language 1
66

77
L1
88
on { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}, CIAO
9-
# "CIAO" can also be "HI" or "BYE" (default HI).
10-
# "entering" is a synonym for "on" with no HI/BYE/CIAO.
9+
# "CIAO" can also be "HI" or "BYE" (default BYE).
10+
# "leaving" is a synonym for "on" with no HI/BYE/CIAO.
1111
whenever { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
1212
# the same as the "on ... CIAO" line
13-
leaving { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
13+
entering { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
1414

1515
# PATTERNS AND ACTIONS
1616

@@ -26,7 +26,8 @@ XML::Axk::Core::L1 - ack-like XML processor, language 1
2626

2727
- `BYE`
2828

29-
After all of the node's children have been processed.
29+
After all of the node's children have been processed. This is the default
30+
so that you have the text content of the node available for inspection.
3031

3132
- `CIAO`
3233

@@ -56,13 +57,15 @@ When an `<action>` is running, it has access to predefined variables
5657
that hold the state of the element being matched. This is similar to `$0`,
5758
`$1`, ... in awk.
5859

60+
At present, L1 uses [XML::DOM](https://metacpan.org/pod/XML::DOM).
61+
5962
- **$D**
6063

61-
The current XML document
64+
The current XML document ([XML::DOM::Document](https://metacpan.org/pod/XML::DOM::Document))
6265

6366
- **$E**
6467

65-
The XML element that was matched
68+
The XML element that was matched ([XML::DOM::Element](https://metacpan.org/pod/XML::DOM::Element))
6669

6770
- **$NOW**
6871

Makefile.PL

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ WriteMakefile(
5252
'parent' => '0',
5353
'rlib' => '0',
5454
'Test::More' => '0.92',
55+
'Test::Exception' => '0',
5556
'Test::Kit' => '0',
5657
'Test::Class' => '0',
5758
'Test::Class::Load' => '0',

lib/XML/Axk/L1.pm

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,12 @@ sub post_all :prototype(&) {
7777
## whether the element described in
7878
## core-parameter hash %CPs matches.
7979
## @params optional when If provided, when to run the action:
80-
## HI, BYE, or CIAO. Default is HI.
80+
## HI, BYE, or CIAO. Default is BYE.
8181
sub add_to_worklist {
8282
my $sandbox = _sandbox or croak "Can't find sandbox";
83-
#say "add_to_worklist args: ", Dumper(\@_);
8483
my ($drAction, $refPattern, $when) = @_;
85-
$when = $when // HI; # only on entry, by default
84+
85+
$when = $when // BYE; # only on exit, by default
8686
$refPattern = \( my $temp = $refPattern ) unless ref($refPattern);
8787

8888
push @{$sandbox->worklist}, [$refPattern, $drAction, $when];
@@ -98,42 +98,41 @@ sub run :prototype(&;$) {
9898
return @_;
9999
} #run()
100100

101-
# pattern-first style - on {} run {} [when (default HI)];
101+
# pattern-first style - on {} run {} [when];
102+
# The default [when] is BYE so that the text content of the node is
103+
# available --- at HI, all you have are the attributes.
102104
sub on :prototype(&@) {
103105
my ($drMakeMatcher, $drAction, $when) = @_;
104106

105-
#say "MakeMatcher: ", Dumper($drMakeMatcher);
106107
my $matcher = &$drMakeMatcher;
107-
#say "Matcher: ", Dumper($matcher);
108-
109108
@_=($drAction, $matcher, $when);
110109
goto &add_to_worklist;
111110
} # on()
112111

113-
# pattern-first style, sugar for symmetry with whenever() and leaving()
114-
sub entering :prototype(&@) {
112+
# pattern-first style, sugar for symmetry with whenever() and entering()
113+
sub leaving :prototype(&@) {
115114
goto &on
116115
} #entering()
117116

118-
# pattern-first style, common implementation for BYE and CIAO
119-
sub _leaving_whenever_impl {
117+
# pattern-first style, common implementation for HI and CIAO
118+
sub _entering_whenever_impl {
120119
croak "Too many arguments" if $#_>2;
121120
my ($when, $drMakeMatcher, $drAction) = @_;
122121
my $matcher = &$drMakeMatcher;
123122
@_=($drAction, $matcher, $when);
124123
goto &add_to_worklist;
125124
} #_leaving_whenever_impl()
126125

127-
# pattern-first style, specific to leaving nodes (BYE) - leaving {} run {};
128-
sub leaving :prototype(&@) {
129-
unshift @_, BYE;
130-
goto &_leaving_whenever_impl;
126+
# pattern-first style, specific to entering nodes (HI) - entering {} run {};
127+
sub entering :prototype(&@) {
128+
unshift @_, HI;
129+
goto &_entering_whenever_impl;
131130
} # leaving()
132131

133132
# pattern-first style, specific to hitting nodes (CIAO) - whenever {} run {};
134133
sub whenever :prototype(&@) {
135134
unshift @_, CIAO;
136-
goto &_leaving_whenever_impl;
135+
goto &_entering_whenever_impl;
137136
} # whenever()
138137

139138
# }}}1
@@ -232,11 +231,11 @@ XML::Axk::Core::L1 - ack-like XML processor, language 1
232231
233232
L1
234233
on { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName}, CIAO
235-
# "CIAO" can also be "HI" or "BYE" (default HI).
236-
# "entering" is a synonym for "on" with no HI/BYE/CIAO.
234+
# "CIAO" can also be "HI" or "BYE" (default BYE).
235+
# "leaving" is a synonym for "on" with no HI/BYE/CIAO.
237236
whenever { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
238237
# the same as the "on ... CIAO" line
239-
leaving { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
238+
entering { xpath(q<//item>) } run {say "$NOW: " . $E->getTagName};
240239
241240
=head1 PATTERNS AND ACTIONS
242241
@@ -256,7 +255,8 @@ When the node is first reached, before any of its children are processed
256255
257256
=item C<BYE>
258257
259-
After all of the node's children have been processed.
258+
After all of the node's children have been processed. This is the default
259+
so that you have the text content of the node available for inspection.
260260
261261
=item C<CIAO>
262262
@@ -290,15 +290,17 @@ When an C<< <action> >> is running, it has access to predefined variables
290290
that hold the state of the element being matched. This is similar to C<$0>,
291291
C<$1>, ... in awk.
292292
293+
At present, L1 uses L<XML::DOM>.
294+
293295
=over
294296
295297
=item B<$D>
296298
297-
The current XML document
299+
The current XML document (L<XML::DOM::Document>)
298300
299301
=item B<$E>
300302
301-
The XML element that was matched
303+
The XML element that was matched (L<XML::DOM::Element>)
302304
303305
=item B<$NOW>
304306

0 commit comments

Comments
 (0)